Python - 将字典字典写入CSV

时间:2014-02-24 13:38:56

标签: python csv dictionary ordereddictionary

我是python的新手,并制作了一个简单的刮刀,它将登录到多个分析帐户并将一些数据打印到CSV。我打印到CSV格式的格式是我使用以下代码创建的字典:

import csv
from collections import OrderedDict
import time
def save_file(website, visitors, links, sources):
    date = time.strftime("%d/%m/%Y")
    d = OrderedDict()
    d['Title'] =  website    # website string
    d['Date'] = date         # date string
    d['Vistors'] = visitors  # integer
    d['Links'] = links       # dictionary of links - URL : Clicks
    d['Sources'] = sources   # dictionary of sources - Source: Clicks

    path = os.path.expanduser('~/Desktop/Traffic Report.csv')
    with open(path, 'a') as f:
        writer = csv.DictWriter(f, d, delimiter=',')
        writer.writerow(d)

使用此代码打印到CSV时,网站,日期和访问者单元格效果很好。链接/源单元格(我正在使用漂亮的数据来获取数据)充满了额外的引号和字符,如下所示。

{"['www.example1.com/']": '1', "['www.example2.com']": '1', "['www.example3.com']": '1', "['www.example4.com/']": '3', "['www.example5.com/']": '1'}
{"['Links']": '2', "['Social media']": '5', "['Direct']": '2', "['Searches']": '1'}

有没有办法删除许多这些字符并打印到csv如下: www.example1.com:1,www.example2.com:1,www.example3.com:1 ...

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

您必须自己进行格式化。而不是字典,构建一个字符串:

d['Links'] = ', '.join(['{}: {}'.format(*item) for item in links.items()])
d['Sources'] = ', '.join(['{}: {}'.format(*item) for item in sources.items()])

这会产生link1: count1, link2: count2个结果。

作为旁注,您不需要在此使用OrderedDict对象,只需按照您希望它们编写的顺序给DictWriter一系列键。我还要在循环外一次打开CSV文件:

d = {
    'Title': website,
    'Date': date,
    'Visitors': visitors,
    'Links': ', '.join(['{}: {}'.format(*item) for item in links.items()]),
    'Sources': ', '.join(['{}: {}'.format(*item) for item in sources.items()],
}

path = os.path.expanduser('~/Desktop/Traffic Report.csv')
with open(path, 'a') as f:
    fields = ('Title', 'Date', 'Visitors', 'Links', 'Sources')
    writer = csv.DictWriter(f, fields, delimiter=',')
    writer.writerow(d)

答案 1 :(得分:1)

def convert(dct):
    return ", ".join("%s : %s" % (key, value) for key, value in dct.iteritems())

(如果是Python3.x,则使用.items()代替.iteritems()然后

d['Links'] = convert(links)
d['Sources'] = convert(sources)