[Tutor] Urgent: unicode problems writing CSV file

Steven D'Aprano steve at pearwood.info
Wed Jun 8 13:57:21 EDT 2016


On Wed, Jun 08, 2016 at 01:18:11PM -0400, Alex Hall wrote:
> I never knew that specifying a question is related to a time-sensitive
> project is considered impolite. My apologies.

Your urgency is not our urgency. We're volunteers offerring our time and 
experience for free. Whether you intended it or not, labelling the post 
"urgent" can come across as badgering us:

"Hey, answer my question! For free! And do it now, not when it is 
convenient to you!"

I'm sure that wasn't your intention, but that's how it can come across.


> The type  of the 'info' variable can vary, as I'm pulling it from a
> database with Pyodbc.

/face-palm

Oh vey, that's terrible! Not your fault, but still terrible.

> 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])

Let's break it up into pieces. You build a list:

    [blah blah blah for info in resultInfo]

then write it to the csv file with writerow. That is straightforward.

What's in the list?

    info.encode("utf8") if type(info)is unicode else info

So Python looks at each item, `info`, decides if it is Unicode or not, 
and if it is Unicode it converts it to a byte str using encode, 
otherwise leaves it be.

If it makes you feel better, this is almost exactly the solution I would 
have come up with in your shoes, except I'd probably write a helper 
function to make it a bit less mysterious:

def to_str(obj):
    if isinstance(obj, unicode):
        return obj.encode('uft-8')
    elif isinstance(obj, str):
        return obj
    else:
        # Or maybe an error?
        return repr(obj)

csvWriter.writerow([to_string(info) for info in resultInfo])


-- 
Steve


More information about the Tutor mailing list