开发者

Excel parsing in python and forming dictionary

开发者 https://www.devze.com 2023-03-12 14:48 出处:网络
I have an Excel file with a lot of data in it . i would like to get the info like in a dictionary example the first column of the excel will be the key and the开发者_高级运维 rest of the column will b

I have an Excel file with a lot of data in it . i would like to get the info like in a dictionary example the first column of the excel will be the key and the开发者_高级运维 rest of the column will be the values

Excel:
No name lastname hobby
1  jhon g        fishing
2  mike a        boxing
3  tom  v        sking

is it possible to have it like

dict = {No:1, name:jhon, lastname:g, hobby:fishing},
dict = {No:2, name:mike, lastname:a, hobby:boxing},

i tried converting the excel to csv and tried csv.DictReader it did not work for me is there any other way


Given the following CSV file:

No,name,lastname,hobby
1,jhon,g,fishing
2,mike,a,boxing
3,tom,v,sking

The following code appears to do what you're asking for:

In [1]: import csv

In [2]: for d in csv.DictReader(open('file.txt')): print d
   ...: 
{'hobby': 'fishing', 'lastname': 'g', 'name': 'jhon', 'No': '1'}
{'hobby': 'boxing', 'lastname': 'a', 'name': 'mike', 'No': '2'}
{'hobby': 'sking', 'lastname': 'v', 'name': 'tom', 'No': '3'}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号