Python :: Unicode字符在文件中写入时出现乱码

时间:2016-12-06 11:27:27

标签: python mysql unicode utf-8

我的要求是从mysql数据库中读取一些数据,然后以JSON格式将其写入文件。但是,在写入文件时,unicode数据会出现乱码。

Actual Unicode Name: ぎぎぎは.exe
Name written to file: ã<81>Žã<81>Žã<81>Žã<81>¯.exe

我的数据库已将charset设置为utf8。我打开如下连接:

MySQLdb.connect (host = "XXXXX", user = "XXXXX", passwd = "XXXX", cursorclass=cursors.SSCursor,charset='utf8',use_unicode=True)

outfile打开如下:

for r in data:
 with open("XX.json",'w') as out:
  d={}
  d['name']=r[0]
  d['type']='Work'
  out.write('%s\n' % json.dumps(d, indent=0, ensure_ascii=False).replace('\n', ''))

这是有效的,但如上所述,unicode数据变得混乱。

如果我输入(r [0]),它就会变成'str'。

如果您的解决方案包含使用codes.open函数,编码为'utf-8',那么请帮我添加解码/编码。此方法需要所有数据都是unicode。

我迷失在网上可用的大量解决方案中,但它们都没有对我完全正常工作:(

操作系统详细信息:CentOS 6

>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'
>>>

1 个答案:

答案 0 :(得分:0)

尝试io.open

# -*- coding: utf8 -*-
import json
import io
with io.open('b.txt', 'at', encoding='utf8') as json_file:
  json_file.write(json.dumps({u'ぎぎぎは': 0}, ensure_ascii=False, encoding='utf8'))