for loop question

Robert Dailey rcdailey at gmail.com
Wed Oct 10 18:03:05 EDT 2007


On 10/10/07, Carsten Haese <carsten at uniqsys.com> wrote:
>
> Instead of passing the file object directly to the csv parser, pass in a
> generator that reads from the file and explicitly encodes the strings
> into UTF-8, along these lines:
>
> def encode_to_utf8(f):
>     for line in f:
>         yield line.encode("utf-8")
>
> There may be a fundamental problem with this approach that I can't
> foresee at the moment, but it's worth a try when your alternative is to
> build a Unicode-aware CSV parser from scratch.
>
> Hope this helps,


I did the following:


#####################################################################

def encode_utf8( f ):
    for line in f:
        yield line.encode( "utf-8" )

#####################################################################

def BeginLocalizationParsing( outputDir, inputRoot, inputFile ):
    f = codecs.open( inputFile, "rb", encoding="utf-16" )

    r = csv.reader( encode_utf8( f ) )
    print r.next()

#####################################################################

It worked perfectly! Thank you! I guess I don't have to make a CSV parser
after all :P
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071010/ec34f418/attachment.html>


More information about the Python-list mailing list