#!/usr/bin/python
#-*-Coding:utf-8-*-
#########################################################################
# File Name:addreplace.py
# Author:yuhaitao
# Mail: [email protected]
# Created Time:fri 07:05:13 PM PDT
#########################################################################
Import OS
Import Sys
Import re
Import CGI,CGITB
Import Codecs
Import time
Import Pymysql
Import MySQLdb
Import JSON
Import Codecs
#接收数据
#def Recvfromform ():
form = CGI. Fieldstorage ()
#getURL = Form.getvalue (' Neturl ')
#接受xmlhttp. Open ("GET", "/cgi-bin/sqltohtml.py?q=" +str,true) parameters passed
Getnum = Form.getvalue (' Q ')
def convert_to_json_string2 (contxt,str_ft):
RET = []# lists that need serialization
TMP = {' Contxt ': Contxt,' Footer ': str_ft}# construct a dictionary from each element of data
Ret.append (TMP)
ret = Json.dumps (ret,Indent=4)
return ret
def dbsongname (song_id,cursor):
Sql_song_name ="" Select*From music163 WHERE song_id =%s "" "% (song_id)
#sql_song_name = "" "Select * from music163 WHERE song_id=186016;" "
#print Sql_song_name
Try
Cursor.execute (Sql_song_name)
Results = Cursor.fetchall ()
For rowIn results:
Name = row[2]
ExceptException, E:
Print"DbSong" +STR (e)
Db.rollback ()
Db.close ()
return name
def convert_to_json_string1 (data):
return Json.dumps (data,Indent=4,encoding=' Utf-8 ',Ensure_ascii=False)
Def dbinsert ():
db = MySQLdb.connect ("Bdm273925510.my3w.com","Bdm273925510","Hfdsggsgfs","Bdm273925510_db",charset=' UTF8 ')
cursor = Db.cursor ()
sql ="" Select*From comment163 WHERE liked > 50000 order by liked DESC "" "
Try
Cursor.execute (SQL)
Results = Cursor.fetchall ()
For I, rowInchEnumerate (results):
id = row[0]
song_id = row[1]
Song_name = Dbsongname (song_id,cursor)
TXT = row[2]
Author = row[3]
liked = row[4]
data = {' Contxt ': txt,' Footer ':U ' Serial number: ' +STR (i) +U '--song: ' +song_name+U '--Comment: ' +STR (liked) +U '--Author: ' +author}
#convert_to_json_string2 (Txt,str (i) +song_name+str (liked) +author)
Print convert_to_json_string1 (data)
With Codecs.open (J\\Users\\Acer_haitao\\Desktop \\net.json ", Span style= "color: #008000; Font-weight:bold ">" + ", encoding=" Utf-8 ') as F:
Json.dump (Data,f, Indent=4,encoding= ' Utf-8 ', ensure_ascii=false)
Span style= "color: #000080; Font-weight:bold ">except exception, E:
print " Dbinsert "+str (e)
Db.rollback ()
Db.close ()
Dbinsert ()
Python reads data from the database to the JSON format file