Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql.cursors
- import json
- # tables: check_content_storage, gd_item
- # Connect to the database
- connection = pymysql.connect(host='localhost',
- user='root',
- password='makap_91',
- db='recomend',
- charset='utf8',
- use_unicode=True,
- cursorclass=pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- cursor.execute("SET NAMES utf8;") #or utf8 or any other charset you want to handle
- cursor.execute("SET CHARACTER SET utf8;") #same as above
- cursor.execute("SET character_set_connection=Latin1;") #same as above
- sql = "SELECT * FROM gd_item LIMIT 1;"
- cursor.execute(sql)
- result = cursor.fetchall()
- print(result)
- print(result[0]['name'])
- print(result[0]['name'].encode('utf-8'))
- parsed = json.loads(result)
- finally:
- connection.close()
- if __name__ == "__main__":
- with open("file.txt", 'w') as f:
- f.write(json.dumps(parsed), sort_keys=True, indent=4, separators=(',', ': '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement