python读取第一行设为字典_将csv读入字典,第一行成为名称

在Python中,可以使用`csv.DictReader`将CSV文件读入字典,其中第一行变为键。例如,Name、Surname、Address1等变成字典中的键,每一行数据成为键值对。这样方便地将CSV数据转换为易于访问的对象,无需在行间关联数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值