【问题标题】:reading of csv into dictionary, first line becomes the name将 csv 读入字典,第一行成为名称
【发布时间】:2014-05-19 19:01:25
【问题描述】:

在python中 我有一个 csv 文件,其中有很多参数,例如:

Name, Surname, Address1, Address2, email, etc
Adam1,Smith1,12 Connaugh Rd.,,adamsmith@gmail.com, etc...
Adam2,Smith2,12 Connaugh Rd.,,adamsmith@gmail.com, etc...
Adam3,Smith3,12 Connaugh Rd.,,adamsmith@gmail.com, etc...

我如何阅读它,所以第一行姓名、姓氏、地址 1、地址 2、电子邮件等 成为字典中参数的名称?所以我可以得到

Dict{Name:Adam1,Adam2, Adam3
     Surname: Smith1,Smith2,Smith3
     Address1: 12 Connaugh Rd.,12 Connaugh Rd.,12 Connaugh Rd.
        etc.}

由于我将来会使用它,这是使用 csv 的最佳方式还是有更好的方式?

更新1: 撕裂(行) 给出:

{None: ['\tSales Record Number', 'User Id', 'Buyer Full name', 'Buyer Phone Number', 'Buyer Email', 'Buyer Address 1', 'Buyer Address 2', 'Buyer Town/City', 'Buyer County', 'Buyer Postcode', 'Buyer Country', 'Item Number', 'Item Title', 'Custom Label', 'Quantity', 'Sale Price', 'Included VAT Rate', 'Postage and Packaging', 'Insurance', 'Cash on delivery fee', 'Total Price', 'Payment Method', 'Sale Date', 'Checkout Date', 'Paid on Date', 'Dispatch Date ', 'Invoice date', 'Invoice number', 'Feedback left', 'Feedback received', 'Notes to yourself', 'PayPal Transaction ID', 'Delivery Service', 'Cash on delivery option', 'Transaction ID', 'Order ID', 'Variation Details']}
{None: ['3528', 'steve33559', 'Steven sdf', '45678', 'sdfghj@dfgj.com', '1 sdfgh Road, ', '', 'dfgh', 'dfgh', 'ertyu', 'United Kingdom', '151216259484', 'Small stuff ', '', '1', '\xa311.99', '', '\xa30.00', '\xa30.00', '', '\xa311.99', 'PayPal', '21-Mar-14', '21-Mar-14', '21-Mar-14', '', '', '', 'Yes', '', '', '384858394n5838f48', 'Other 24 Hour Courier', '', '49503847573848', '', '']}
{None: ['3529', 'buyretry13', 'Tariq fhb', '345678', 'buyretry@uk.com', '80 rtyukfd Road', '', 'Manchester', 'wertyuk', 'M16 1KY', 'United Kingdom', '76543283858', 'Apple iPhone 5', '100329', '1', '\xa31.95', '', '\xa30.00', '\xa30.00', '', '\xa31.95', 'PayPal', '21-Mar-14', '21-Mar-14', '21-Mar-14', '', '', '', 'Yes', '', '', '45678723456', 'Royal Mail 2nd Class', '', '3456785737', '', '']}

【问题讨论】:

    标签: python csv dictionary


    【解决方案1】:

    您可以使用zip() 将列转置为行,并将其应用于字典推导以提取第一个元素作为键:

    import csv
    
    with open(yourfile, 'rb') as infile:
        reader = csv.reader(infile)
        result = {c[0]: c[1:] for c in zip(*reader)}
    

    这会生成一个字典,每个字典都将列中的所有条目作为值列表。

    不过,您最好在此处使用csv.DictReader()。这会产生一个每行的dict对象:

    import csv
    
    with open(yourfile, 'rb') as infile:
        reader = csv.DictReader(infile)
        for row in reader:
            print row
    

    其中row 是第一行的{'Name': 'Adam1', 'Surname': 'Smith1', 'Address1': 'Connaugh rd.', ...}{'Name': 'Adam2', 'Surname': 'Smith2', 'Address1': 'Connaugh rd.', ...} 等。DictReader() 对象从 CSV 数据的第一行获取键。

    这将每一行数据作为一个易于访问的对象保存在一起,而不必在不同行之间关联您的数据。

    演示:

    >>> import csv
    >>> sample = '''\
    ... Name,Surname,Address1,Address2,email,etc
    ... Adam1,Smith1,12 Connaugh Rd.,,adamsmith@gmail.com,etc...
    ... Adam2,Smith2,12 Connaugh Rd.,,adamsmith@gmail.com,etc...
    ... Adam3,Smith3,12 Connaugh Rd.,,adamsmith@gmail.com,etc...
    ... '''
    >>> reader = csv.DictReader(sample.splitlines())
    >>> print next(reader)
    {'Surname': 'Smith1', 'Name': 'Adam1', 'Address1': '12 Connaugh Rd.', 'Address2': '', 'etc': 'etc...', 'email': 'adamsmith@gmail.com'}
    >>> print next(reader)
    {'Surname': 'Smith2', 'Name': 'Adam2', 'Address1': '12 Connaugh Rd.', 'Address2': '', 'etc': 'etc...', 'email': 'adamsmith@gmail.com'}
    >>> print next(reader)
    {'Surname': 'Smith3', 'Name': 'Adam3', 'Address1': '12 Connaugh Rd.', 'Address2': '', 'etc': 'etc...', 'email': 'adamsmith@gmail.com'}
    

    【讨论】:

    • 这是一个很好的解决方案,我得到{无:在每一行的开头,我如何摆脱它们,或者用有意义的东西替换?
    • 你能告诉我repr(row) 连续输出吗?我不确定你的意思。
    • 我已经更新了帖子,repr(row) 数据,太大了,不能在这里插入
    • @ipsissimus:是什么代码产生的?您有一个键 (None),每一行都是该键的列表值。
    • 这是 DictReader 的结果,您提供的第二个代码
    猜你喜欢
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多