0

I have a csv file (in.csv)

col1, col2, col3
Kapitän, Böse, Füller
...

and I want to create a list of dictionaries:

a = [{'col1': 'Kapitän',  'col2': 'Böse', 'col3': 'Füller'},{...}]

With Python 3 it's working with

    import codecs
    with codecs.open('in.csv', encoding='utf-8') as f:
        a = [{k: v for k, v in row.items()}
            for row in csv.DictReader(f, skipinitialspace=True)]
    print(a)

(I've got this code from convert csv file to list of dictionaries).

Unfortunately I need this for Python 2, but I don't come along with it.

I tried to understand https://docs.python.org/2.7/howto/unicode.html, but I think I'm too stupid, because

import codecs
f = codecs.open('in.csv', encoding='utf-8')
for line in f:
print repr(line) 

gives me

u'col1,col2,col3\n'
u'K\xe4pten,B\xf6se,F\xfcller\n'
u'\n'

Do you have a solution for Python 2?

There is a similar problem solved here: Creating a dictionary from a csv file? But with the marked solution I get ('K\xc3\xa4pten', 'B\xc3\xb6se', 'F\xc3\xbcller'). Maybe it's easy to edit it for getting [{u'col1': u'K\xe4pten', u'col2': u'B\xf6se', u'col3': u'F\xfcller'}]?

Community
  • 1
  • 1
flowerflower
  • 327
  • 1
  • 3
  • 10
  • Possible duplicate of [Creating a dictionary from a csv file?](http://stackoverflow.com/questions/6740918/creating-a-dictionary-from-a-csv-file) – innoSPG Sep 13 '16 at 20:18
  • Read [understanding repr](http://stackoverflow.com/questions/7784148/understanding-repr-function-in-python), whay you're seeing is the representation of those unicode strings (the `u` prefix etc.). With `{k: v for k, v in row.items()}` you're basically just recreating the dictionaries. – Ilja Everilä Sep 13 '16 at 20:25
  • Not sure what you mean by "I don't come along with it", but at least for 2.7, you can still use a DictReader: https://docs.python.org/2/library/csv.html#csv.DictReader. – Albert Rothman Sep 13 '16 at 20:35
  • @innoSPG: Thank you for linking! But for me it's a bit different: With the linked solution I get `{'K\xc3\xa4pten': 'B\xc3\xb6se', 'col1': 'col2'}` @ilja: Thank you for the hint with the 'u's. Now I'm understanding that the code above is working fine. @albert: I just wanted to say, that I've not been able to edit the code mentioned in these examples for my use because I was overstrained. – flowerflower Sep 14 '16 at 15:17

2 Answers2

1

you can leverage the csv lib for the job.

import csv

li_of_dicts = []
with open('in.csv', 'r') as infile:
     reader = csv.DictReader(infile, encoding='utf-8')
     for row in reader:
         li_of_dicts.append(row)
Mike Tung
  • 4,735
  • 1
  • 17
  • 24
0

for print use print line instead print repr(line)

and for dict i use this solution

https://docs.python.org/2/library/csv.html#csv-examples

The csv module doesn’t directly support reading and writing Unicode

import codecs
import csv


def utf_8_encoder(unicode_csv_data):
    for line in unicode_csv_data:
        yield line.encode('utf-8')

def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
    # csv.py doesn't do Unicode; encode temporarily as UTF-8:
    csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
                            dialect=dialect, **kwargs)
    for row in csv_reader:
        # decode UTF-8 back to Unicode, cell by cell:
        yield [unicode(cell, 'utf-8') for cell in row]

with codecs.open('in.csv', encoding='utf-8') as f:
    reader = unicode_csv_reader(f)
    keys = [k.strip() for k in reader.next()]
    result = []
    for row in reader:
        d=dict(zip(keys, row))
        result.append(d)

    for d in result:
        for k, v in d.iteritems():
            print k, v
    print result
flowerflower
  • 327
  • 1
  • 3
  • 10
OrangeFish
  • 32
  • 5