Reading a UTF8 CSV file with Python

后端 未结 9 1485
青春惊慌失措
青春惊慌失措 2020-11-22 12:20

I am trying to read a CSV file with accented characters with Python (only French and/or Spanish characters). Based on the Python 2.5 documentation for the csvreader (http://

相关标签:
9条回答
  • 2020-11-22 13:01

    The .encode method gets applied to a Unicode string to make a byte-string; but you're calling it on a byte-string instead... the wrong way 'round! Look at the codecs module in the standard library and codecs.open in particular for better general solutions for reading UTF-8 encoded text files. However, for the csv module in particular, you need to pass in utf-8 data, and that's what you're already getting, so your code can be much simpler:

    import csv
    
    def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs):
        csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs)
        for row in csv_reader:
            yield [unicode(cell, 'utf-8') for cell in row]
    
    filename = 'da.csv'
    reader = unicode_csv_reader(open(filename))
    for field1, field2, field3 in reader:
      print field1, field2, field3 
    

    PS: if it turns out that your input data is NOT in utf-8, but e.g. in ISO-8859-1, then you do need a "transcoding" (if you're keen on using utf-8 at the csv module level), of the form line.decode('whateverweirdcodec').encode('utf-8') -- but probably you can just use the name of your existing encoding in the yield line in my code above, instead of 'utf-8', as csv is actually going to be just fine with ISO-8859-* encoded bytestrings.

    0 讨论(0)
  • 2020-11-22 13:02

    The link to the help page is the same for python 2.6 and as far as I know there was no change in the csv module since 2.5 (besides bug fixes). Here is the code that just works without any encoding/decoding (file da.csv contains the same data as the variable data). I assume that your file should be read correctly without any conversions.

    test.py:

    ## -*- coding: utf-8 -*-
    #
    # NOTE: this first line is important for the version b) read from a string(unicode) variable
    #
    
    import csv
    
    data = \
    """0665000FS10120684,SD1200IS,Appareil photo numérique PowerShot de 10 Mpx de Canon avec trépied (SD1200IS) - Bleu
    0665000FS10120689,SD1200IS,Appareil photo numérique PowerShot de 10 Mpx de Canon avec trépied (SD1200IS) - Gris
    0665000FS10120687,SD1200IS,Appareil photo numérique PowerShot de 10 Mpx de Canon avec trépied (SD1200IS) - Vert"""
    
    # a) read from a file
    print 'reading from a file:'
    for (f1, f2, f3) in csv.reader(open('da.csv'), dialect=csv.excel):
        print (f1, f2, f3)
    
    # b) read from a string(unicode) variable
    print 'reading from a list of strings:'
    reader = csv.reader(data.split('\n'), dialect=csv.excel)
    for (f1, f2, f3) in reader:
        print (f1, f2, f3)
    

    da.csv:

    0665000FS10120684,SD1200IS,Appareil photo numérique PowerShot de 10 Mpx de Canon avec trépied (SD1200IS) - Bleu
    0665000FS10120689,SD1200IS,Appareil photo numérique PowerShot de 10 Mpx de Canon avec trépied (SD1200IS) - Gris
    0665000FS10120687,SD1200IS,Appareil photo numérique PowerShot de 10 Mpx de Canon avec trépied (SD1200IS) - Vert
    
    0 讨论(0)
  • 2020-11-22 13:02

    Had the same problem on another server, but realized that locales are messed.

    export LC_ALL="en_US.UTF-8"
    

    fixed the problem

    0 讨论(0)
提交回复
热议问题