为列表编码 UTF-8
Encode UTF-8 for list
我正在使用 selenium 从 javascript 对象中检索列表。
search_reply = driver.find_element_by_class_name("ac_results")
尝试写入 csv 时,出现此错误:
Traceback (most recent call last):
File "insref_lookup15.py", line 54, in <module>
wr_insref.writerow(instrument_name)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 22: ordinal not in range(128)
我已经尝试在两者上放置 .encode("utf-8"):
search_reply = driver.find_element_by_class_name("ac_results").encode("utf-8")
和
wr_insref.writerow(instrument_name).encode("utf-8")
但我刚收到消息
AttributeError: 'xxx' object has no attribute 'encode'
您需要对列表中的 个元素进行编码:
wr_insref.writerow([v.encode('utf8') for v in instrument_name])
csv
模块文档有一个 Examples section 更详细地介绍了编写 Unicode 对象,包括实用程序 类 以自动处理此问题。
我正在使用 selenium 从 javascript 对象中检索列表。
search_reply = driver.find_element_by_class_name("ac_results")
尝试写入 csv 时,出现此错误:
Traceback (most recent call last):
File "insref_lookup15.py", line 54, in <module>
wr_insref.writerow(instrument_name)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 22: ordinal not in range(128)
我已经尝试在两者上放置 .encode("utf-8"):
search_reply = driver.find_element_by_class_name("ac_results").encode("utf-8")
和
wr_insref.writerow(instrument_name).encode("utf-8")
但我刚收到消息
AttributeError: 'xxx' object has no attribute 'encode'
您需要对列表中的 个元素进行编码:
wr_insref.writerow([v.encode('utf8') for v in instrument_name])
csv
模块文档有一个 Examples section 更详细地介绍了编写 Unicode 对象,包括实用程序 类 以自动处理此问题。