简体   繁体   English

Python-将字典词典编写为CSV

[英]Python - Writing a dictionary of dictionaries to CSV

I'm new to python and made a simple scraper that will log into several analytics accounts and print some data to a CSV. 我是python的新手,做了一个简单的刮板,它将登录到多个分析帐户并将一些数据打印到CSV。 The format I'm printing to CSV in is a dictionary that I create with the following code: 我要打印为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)

When I print to CSV using this code, the site, date, and visitors cells work great. 当我使用此代码打印到CSV时,站点,日期和访问者单元格都可以正常工作。 The links/source cells (data I'm using beautifulsoup to scrape) are full of extra quotation marks and characters as seen below. 链接/源单元格(我正在使用beautifulsoup抓取的数据)充满了多余的引号和字符,如下所示。

{"['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'}

Is there any way to remove many of these characters and print to csv as: www.example1.com : 1, www.example2.com : 1, www.example3.com : 1... 有什么方法可以删除许多这些字符并以以下格式打印到csv:www.example1.com:1,www.example2.com:1,www.example3.com:1 ...

Any help would be greatly appreciated! 任何帮助将不胜感激!

You'd have to do the formatting yourself. 您必须自己进行格式化。 Instead of a dictionary, build a string: 代替字典,构建一个字符串:

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

This produces link1: count1, link2: count2 results. 这将产生link1: count1, link2: count2结果。

As a sidenote, you don't need to use an OrderedDict object here, just give the DictWriter a sequence of keys in the order you want them written instead. DictWriter一下,您无需在此处使用OrderedDict对象,只需按希望它们编写的顺序为DictWriter一个键序列即可。 I'd also open the CSV file just once outside the loop: 我还要在循环外一次打开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)
def convert(dct):
    return ", ".join("%s : %s" % (key, value) for key, value in dct.iteritems())

(use .items() instead of .iteritems() if Python3.x) and then (如果使用.items()使用.items()而不是.iteritems() ),然后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM