我正在尝试将 unicode 数据(实际数据包含德语字符)写入文件,但出现错误:
Traceback (most recent call last):
File "C:\Python27\extract_osm_road_nw.py", line 76, in <module>
file.write(str(list_way_id[index][2][i][1]))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 12: ordinal not in range(128)
代码是:
## writing the data in a file
## The data format is:
## A list of: [tuple(way ref id,list[tuple(node id, lat, long)],list[tuple(key,value)]),.....]
## For example: [(u'72439830', [(u'298094414', u'52.4626304', u'10.5579578'), (u'860126050', u'52.4626762', u'10.5576574')], [(u'name', u'General-BeckStra\xdfe')]),.....]
with codecs.open("extracted_osm_file.csv", "w", encoding="utf-8") as file:
for index in range(len(list_way_id)):
file.write("Way ID Ref No: ")
file.write(str(list_way_id[index][0]))## points to the way id ref no
file.write("\n")
file.write("Node reference id, latitude, longitude: ")
file.write("\n")
for i in range(len(list_way_id[index][1])):
file.write(str(list_way_id[index][1][i][0]))
file.write(",")
file.write(str(list_way_id[index][1][i][1]))
file.write(",")
file.write(str(list_way_id[index][1][i][2]))
file.write("\n")
for i in range(len(list_way_id[index][2])):
file.write(str(list_way_id[index][2][i][0]))
file.write(",")
file.write(str(list_way_id[index][2][i][1]))
file.write("\n")
file.close()