for event in events:
case = {'Artist': item['Artist'], 'Date': item['Date'], 'Time': item['Time'], 'Venue': item['Venue'],
'Address': item['Address'], 'Coordinates': item['Coordinates']}
item[event] = case
with open("testScrape.json", "w") as writeJSON:
json.dump(item, writeJSON, ensure_ascii=False)
代码出现错误,我收到以下错误信息:
Traceback (most recent call last):
File "/Users/s/PycharmProjects/hi/BandsintownWebScraper.py", line 126, in <module>
json.dump(item, writeJSON, ensure_ascii=False)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 190, in dump
fp.write(chunk)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe6' in position 7: ordinal not in range(128)
我尝试使用:
json.dump(item, writeJSON, ensure_ascii=False).decode('utf-8')
并且:
json.dump(item, writeJSON, ensure_ascii=False).encode('utf-8')
没有成功。我相信链接中的ï字符导致了失败。有人能简要介绍一下正在发生什么,编码/解码的含义以及如何解决这个问题吗?
Latin1
、CP1250
。 - furasutf-8
、latin1
等,以使用更少的空间。编码后的字符可能使用1个字节,其他字符可能使用2个或更多字节 - 这比每个字符使用8个字节要节省空间。当您获取它时,您必须将其转换(解码)回Unicode,以便Python可以使用它。 - furasopen(..., encode='utf-8')
中设置编码。 - furas