如何将convert csv转换为词典列表(UTF-8)?

时间:2016-09-13 20:13:43

标签: python list python-2.7 csv dictionary

我有一个csv文件(in.csv)

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

我想创建一个词典列表:

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

使用Python 3,它正在使用

    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)

(我已经从convert csv file to list of dictionaries获得了此代码。)

不幸的是,我需要这个用于Python 2,但我不喜欢它。

我试图了解https://docs.python.org/2.7/howto/unicode.html,但我觉得我太傻了,因为

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

给了我

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

你有Python 2的解决方案吗?

这里解决了类似的问题:Creating a dictionary from a csv file?但是通过标记的解决方案,我得到('K\xc3\xa4pten', 'B\xc3\xb6se', 'F\xc3\xbcller')。可能很容易编辑它以获取[{u'col1': u'K\xe4pten', u'col2': u'B\xf6se', u'col3': u'F\xfcller'}]

2 个答案:

答案 0 :(得分:1)

你可以利用csv lib来完成这项工作。

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)

答案 1 :(得分:0)

打印使用print line代替print repr(line)

对于dict我使用此解决方案

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

csv模块不直接支持读取和写入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