将CSV读入字典,第一行成为名称
在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:
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', '', '']}
1 个回答
3
你可以使用 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()
会更好。这个方法会为每一行生成一个字典对象:
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'}