我在文本框中输入了这个文本
‘f’fdsfs’`124539763~!@#$%^’’;’””::’
我正在转向json然后它来了
"\\u2018f\\u2019fdsfs\\u2019`124539763~!@#$%^\\u2019\\u2019;\\u2019\\u201d\\u201d::\\u2019e"
现在,当我编写csv文件时,我收到此错误
'ascii' codec can't encode character u'\u2018' in position 0: ordinal not in range(128)
csv.writer(data)
我尝试了所有data.encode('utf-8')
data.decode('unicode-escape')
,但没有效果
答案 0 :(得分:1)
csv模块不支持使用unicode https://github.com/jdunck/python-unicodecsv
虽然我不确定\ u2018是utf-8 charset的一部分
x = "\\u2018f\\u2019fdsfs..."; j = json.loads('"' + x + '"'); print j.encode('cp1252')
‘f’fdsfs...
请注意,它是编码为cp1252
>>> import unicodecsv as csv #https://github.com/jdunck/python-unicodecsv
>>> x = "\\u2018f\\u2019fdsfs..."; j = json.loads('"' + x + '"');
>>> with open("some_file.csv","wb") as f:
... w = csv.writer(f,encoding="cp1252")
... w.writerow([j,"normal"])
...
>>>
这是csv文件:https://www.dropbox.com/s/m4gta1o9vg8tfap/some_file.csv