问题
I'm using python 3.4 and I'm trying to write a list of names to a text file. The list is as follows:
my_list = ['Dejan Živković','Gregg Berhalter','James Stevens','Mike Windischmann',
'Gunnar Heiðar Þorvaldsson']
I use the following code to export the list:
file = open("/Users/.../Desktop/Name_Python.txt", "w")
file.writelines( "%s\n" % item for item in my_list )
file.close()
But it does not work. Python seems not to like non-ASCII characters and gives me the following errors:
"UnicodeEncodeError: 'ascii' codec can't encode character '\u017d' in position 6: ordinal not in range(128)"
Do you know if there is a way to solve this problem ? Maybe it's possible to write files in UTF-8 / unicode ?
回答1:
The issue is that the file is getting openned with ascii
encoding (which might be what is returned by locale.getpreferredencoding() for your environment). You can try openning with the correct encoding (maybe utf-8
) . Also, you should use with
statement so that it handles closing the file for you.
For Python 2.x , you can use codecs.open() function instead of open()
-
with codecs.open("/Users/.../Desktop/Name_Python.txt", "w",encoding='utf-8') as file:
file.writelines( "%s\n" % item for item in my_list )
For Python 3.x , you can use the built-in function open() , which supports encoding
argument. Example -
with open("/Users/.../Desktop/Name_Python.txt", "w",encoding='utf-8') as file:
file.writelines( "%s\n" % item for item in my_list )
回答2:
try this:
>>> my_list = ['Dejan Živković','Gregg Berhalter','James Stevens','Mike Windischmann' ,'Gunnar Heiðar Þorvaldsson']
>>> f = open("/Users/.../Desktop/Name_Python.txt", "w")
>>> for x in my_list:
... f.write("{}\n".format(x))
...
>>> f.close()
回答3:
Best method would be to play with unicodes
my_list = [u'Dejan \u017Divkovi\u0107','Gregg Berhalter','James Stevens','Mike Windischmann'
,u'Gunnar Hei\u00F0ar \u00FEorvaldsson']
print my_list[0]
Output:Dejan Živković
回答4:
Try using UTF-8 encoding. You can start by putting # -- coding: utf-8 -- at the top of your .py file.
来源:https://stackoverflow.com/questions/33255846/python-write-a-list-with-non-ascii-characters-to-a-text-file