使用不按字母顺序排列的键将dict写入csv文件

时间:2016-11-11 18:31:41

标签: python csv dictionary

我知道当使用python将dicts写入csv文件时,标题将按字母顺序排列。那么有没有一种方法可以用我想要的顺序编写标题?

tsv文件的代码和输出如下。

我有一个词典:

my_data = {"name": name[:], "city": city[:], "state": state[:],
           "stars": stars[:], "review_count": review_count[:],
           "main_category": new_cat[:]}

我使用以下代码在csv文件中编写它们:

with open('test.tsv','w') as file:
    writer = csv.writer(file, delimiter='\t')
    writer.writerow(my_data.keys())
    for row in zip(*my_data.values()):
        writer.writerow(list(row))

tsv文件输出的前几行如下:

city    review_count    name    main_category   state   stars
Los Angeles 2   Southern California Medical Group   Medical Centers CA  3.5
Cambridge   4   Harvard Square Shiatsu  Massage MA  4.0
Kitchener   2   Faith & Glory Collective    Tattoo  ON  4.0

您可以看到它们按字母顺序排列,但我真正想要的是它们可以按照my_data中的键的顺序放置,如下所示:

name    city    state    stars    review_count    main_category

3 个答案:

答案 0 :(得分:1)

仅限一条记录。

import csv

output=open('temp.csv', 'w')
outputCSV = csv.DictWriter(output, delimiter = '\t', \
    fieldnames = [ 'name', 'city', 'state', 'stars', 'review_count', 'main_category' ] )

outputCSV.writerow( {
    'name': 'Southern Cal Med Group',
    'city': 'Los Angeles',
    'state': 'CA',
    'review_count': '2',
    'main_category': 'medical',
    'stars': '3.5',
    } )

output.close()

答案 1 :(得分:0)

以下显示了按照您想要的顺序将数据输出到tsv文件的几种方法:

with open('test.tsv', 'wb') as file:
    FIELDNAMES = 'name city state stars review_count main_category'.split()
    writer = csv.writer(file, delimiter='\t')
    writer.writerow(FIELDNAMES)
    writer.writerows(row for row in zip(*(my_data[key] for key in FIELDNAMES)))

正如其他人所建议的那样,也可以使用csv.DictWriter

来完成
with open('test.tsv', 'wb') as file:
    FIELDNAMES = 'name city state stars review_count main_category'.split()
    writer = csv.DictWriter(file, delimiter='\t', fieldnames=FIELDNAMES)
    writer.writeheader()
    writer.writerows(dict(zip(FIELDNAMES, row))
                         for row in zip(*(my_data[key] for key in FIELDNAMES)))

正如您所看到的那样,两个代码的数量相同,尽管第一个非DictWriter版本的效率更高,因为为每个row生成数据需要更少的工作量,因此它可能会快一点。

无论使用哪种类型的编写器,创建的test.tsv文件的内容都将完全相同,如下所示(其中代表制表符分隔符):

name→city→state→stars→review_count→main_category
Southern California Medical Group→Los Angeles→CA→3.5→2→Medical Centers
Harvard Square Shiatsu→Cambridge→MA→4.0→4→Massage
Faith & Glory Collective→Kitchener→ON→4.0→2→Tattoo

注意:如果您使用的是Python 2,则打开输出文件以使用模式'wb'进行编写。对于Python 2,您应该使用模式'w'并添加newline=''关键字参数。

答案 2 :(得分:0)

如果您有每行字段的字典,请使用DictWriter。它可以选择填写缺失字段的默认值,或忽略输出中不需要的额外字段。

示例:

import csv

# Sample data converted to a list of lines.
datalines = '''\
Los Angeles,2,Southern California Medical Group,Medical Centers,CA,3.5
Cambridge,4,Harvard Square Shiatsu,Massage,MA,4.0
Kitchener,2,Faith & Glory Collective,Tattoo,ON,4.0
'''.splitlines()

# Specify the fields you want in the output.
# If your dictionaries have fields you don't want in the output, use extrasaction='ignore'
fields = 'name city state stars review_count main_category'.split()

# Python 3, use newline=''.  Python 2 use 'wb' instead.
with open('test.tsv','w',newline='') as file:
    writer = csv.DictWriter(file, fields, delimiter='\t', extrasaction='ignore')

    writer.writeheader() # Writes headers specified by "fields"

    for line in datalines:
        # Converts input line into a dict in OP's format.
        my_data = dict(zip('city review_count name main_category state stars'.split(),line.split(',')))
        print(my_data)
        writer.writerow(my_data)

用于验证字典的输出采用OP的格式:

{'stars': '3.5', 'review_count': '2', 'name': 'Southern California Medical Group', 'city': 'Los Angeles', 'state': 'CA', 'main_category': 'Medical Centers'}
{'stars': '4.0', 'review_count': '4', 'name': 'Harvard Square Shiatsu', 'city': 'Cambridge', 'state': 'MA', 'main_category': 'Massage'}
{'stars': '4.0', 'review_count': '2', 'name': 'Faith & Glory Collective', 'city': 'Kitchener', 'state': 'ON', 'main_category': 'Tattoo'}

输出到test.tsv(→选项卡):

name→city→state→stars→review_count→main_category
Southern California Medical Group→Los Angeles→CA→3.5→2→Medical Centers
Harvard Square Shiatsu→Cambridge→MA→4.0→4→Massage
Faith & Glory Collective→Kitchener→ON→4.0→2→Tattoo