python讀取第一行設為字典_將csv讀入字典,第一行成為名稱

 2023-12-10 阅读 26 评论 0

摘要:在python中,我有一個csv文件,其中包含很多參數,例如:Name, Surname, Address1, Address2, email, etcAdam1,Smith1,12 Connaugh Rd.,,adamsmith@gmail.com, etc...Adam2,Smith2,12 Connaugh Rd.,,adamsmith@gmail.com, etc...Adam3,Smith

在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的最佳方法還是有更好的方法?

Update1:??ripr(row)給出:

{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', '', '']}

解決方案

您可以用于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'}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/194187.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息