python csv encoding writerow -


using web2py on windows 2008 server have following problem

i creating csv document json , when writting list file following error. crashes on csv writerow

<type 'exceptions.unicodeencodeerror'> 'ascii' codec can't encode character u'\\u010c' 

it works ok on computer. windows 7 on server have encoding problems

any suggestions? thank you

my code creating file following

datadict = json.loads(data.replace("'", "\"")) path = path scriptname = os.path.join(path, id + 'script.txt') file = open(scriptname, 'wb')  output = csv.writer(file, delimiter='\t')  ##month hours file.write("begin month_hours \r\n") file.write("delavec    mesec    month_hours_min    month_hours_max\r\n") rec in datadict["mandatory"]:     output.writerow(rec) file.write("\r\nend month_hours \r\n") 

json strings unicode values , need encoded when writing csv file. if don't explicitly, python use ascii codec. that's fine if data contains text in ascii range fails when encounter data beyond that.

pick different encoding , encode explicitly; utf-8 encoding pick:

for rec in datadict["mandatory"]:     output.writerow([unicode(c).encode('utf8') c in rec]) 

i first convert values unicode(), in case have data in there that's not unicode() value; numbers or booleans or none example. result explicitly encoded utf-8.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -