我尝试了两种方法来解决这个问题,但是都会导致另一个错误。首先尝试使用encode
,另一种方法是尝试使用strip
(#),假设这就是导致错误的问题:
"color":rcolor,"text_color":tcolor})
File "/usr/lib/python2.7/csv.py", line 152, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 78: ordinal not in range(128)
我的代码现在看起来像这样:
routes = db.routes.find()
for route in routes:
try:
color = route["properties"]["color"]
color = color.strip('#')
print(color)
tcolor = route["properties"]["tcolor"]
tcolor = tcolor.strip('#')
print(tcolor)
except KeyError:
color = "0000FF"
tcolor = ""
writer.writerow({"route_id":route["route_id"],
"agency_id":route["properties"]["agency_id"],...,
"route_color":color,"route_text_color":tcolor})
我不太确定为什么它一直出现Unicode错误...