Encode UTF-8 for list

时间:2015-05-24 21:48:30

标签: python python-2.7 encoding utf-8

I'm using selenium to retrieve a list from a javascript object.

search_reply = driver.find_element_by_class_name("ac_results")

When trying to write to csv, I get this error:

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)

I have tried placing .encode("utf-8") on both:

search_reply = driver.find_element_by_class_name("ac_results").encode("utf-8")

and

wr_insref.writerow(instrument_name).encode("utf-8")

but I just get the message

AttributeError: 'xxx' object has no attribute 'encode'

1 个答案:

答案 0 :(得分:3)

You need to encode the elements in the list:

wr_insref.writerow([v.encode('utf8') for v in instrument_name])

The csv module documentation has an Examples section that covers writing Unicode objects in more detail, including utility classes to handle this automatically.