csv - Unicode Error on Python -


so i'm trying make table csv table has unicode characters in it:

with open('test1.csv', 'w') csvfile:     writer = csv.writer(csvfile)     [writer.writerow(r) r in table] 

i error every time try run program, though:

unicodeencodeerror: 'ascii' codec can't encode characters in position 8-10: ordinal not in range(128) 

how fix this?

assuming you're using python2:

with open('test1.csv', 'wb') csvfile:     writer = csv.writer(csvfile)     r in table:         writer.writerow([x.encode('utf-8') x in r]) 

of course, when open csv file, need decode using same encoding:

with open('test1.csv') csvfile:     reader = csv.reader(csvfile.decode('utf-8')) 

(nb: if used python3, none of necessary - original example work fine is).


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -