P A N
P A N

Reputation: 5922

Encode UTF-8 for list

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'

Upvotes: 0

Views: 385

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121554

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.

Upvotes: 3

Related Questions