[Tutor] Urgent: unicode problems writing CSV file

Alex Hall ahall at autodist.com
Wed Jun 8 13:18:11 EDT 2016


I never knew that specifying a question is related to a time-sensitive
project is considered impolite. My apologies.

The type  of the 'info' variable can vary, as I'm pulling it from a
database with Pyodbc. I eventually found something that works, though I'm
not fully sure why or how.

  csvWriter.writerow([info.encode("utf8") if type(info)is unicode else info
for info in resultInfo])

where resultInfo is an array holding the values from a row of database
query results, in the order I want them in.


On Wed, Jun 8, 2016 at 1:08 PM, Peter Otten <__peter__ at web.de> wrote:

> Alex Hall wrote:
>
> Marking your posts is generally considered impolite and will not help you
> get answers sooner than without it.
>

> I'm working on a project that writes CSV files, and I have to get it done
> > very soon. I've done this before, but I'm suddenly hitting a problem with
> > unicode conversions. I'm trying to write data, but getting the standard
> > cannot encode character: ordinal not in range(128)
> >
> > I've tried
> > str(info).encode("utf8")
> > str(info).decode(utf8")
> > unicode(info, "utf8")
> > csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword
> > argument
> >
> > What else can I do? As I said, I really have to get this working soon,
> but
> > I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks.
>
> What's the type of "info"?
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Alex Hall
Automatic Distributors, IT department
ahall at autodist.com


More information about the Tutor mailing list