将多个CSV列的值返回到Python字典? [英] Returning values from multiple CSV columns to Python dictionary?
本文介绍了将多个CSV列的值返回到Python字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将其他CSV列调用到我的Python字典下面。
该示例适用于返回一组值(列)需要返回不相邻的2列的值。
import csv
#设置csv文件和路径
allrows = list(csv.reader(open('C:/Data/Library/Peter/123.csv')))
#提取第一行为用于列字典的键
columns = dict([(x [0],x [1:])for x in zip(* allrows)])
#第4列中具有特定标准的所有行
matchingrows = [枚举(列[[Status]]中的(rownum,value)的rownum)if value =='Keep']
打印映射'book_ref'] .__ getitem__,matchingrows)
**样例123.csv **
book_id类型book_ref状态
607842 3 9295保留
607844 4 7643保持
607846 3 2252退休
607856 3 7588保留
返回第3列的值正确
['9644','4406','7643','2252','7588']
但是如何从列1和3返回值?
['607842':'4406','607844':'7643','607846':'2252','607856':'7588']
我也试过这个,但我不能得到我想要的。
## import csv
## with open('C:/Data/Library/Peter/123.csv ')as f:
## reader = csv.DictReader(f)
## for reader in reader:
## print row
'''
{ 'status':'Retire','TYPE':'3','book_ref':'2397','book_id':'607838'}
{'Status':'Keep','TYPE':' 12','book_ref':'9644','book_id':'607839'}
{'Status':'Retire','TYPE':'4','book_ref':'9295','book_id ':'607841'}
{'Status':'Keep','TYPE':'3','book_ref':'4406','book_id':'607842'}
{'Status ':'Retire','TYPE':'4','book_ref':'1798','book_id':'607843'}
{'status':'Keep','TYPE':' ,'book_ref':'7643','book_id':'607844'}
{'Status':'Retire','TYPE':'3','book_ref':'6778','book_id': '607845'}
{'Status':'Keep','TYPE':'3','book_ref':'2252','book_id':'607846'}
{'Status' ''','TYPE':'4','book_ref':'7910','book_id':'607855'}
{'Status' book_ref':'7588','book_id':'607856'}
解决方案>
或尝试:
import csv
result = {}
with open C:/ temp123.csv')as f:
reader = csv.DictReader(f)
读取行:
if row ['Status'] =='Keep' :
result.update({row ['book_id']:row ['book_ref']})
$ b b
它会产生:
{'607842':'9295','607844':'7643' 607856':'7588'}
I am trying to call additional CSV columns to my Python dictionary below.
The example works fine for the return of one set of values (column), but I need to return values for 2 columns that are not next to each other.
import csv
# set csv file and path
allrows=list(csv.reader(open('C:/Data/Library/Peter/123.csv')))
# Extract the first row as keys for a columns dictionary
columns=dict([(x[0],x[1:]) for x in zip(*allrows)])
# Then extracting column 3 from all rows with a certain criterion in column 4
matchingrows=[rownum for (rownum,value) in enumerate(columns['Status']) if value == 'Keep']
print map(columns['book_ref'].__getitem__, matchingrows)
**sample from 123.csv**
book_id TYPE book_ref Status
607842 3 9295 Keep
607844 4 7643 Keep
607846 3 2252 Retire
607856 3 7588 Keep
Returns values from column 3 correctly
['9644', '4406', '7643', '2252', '7588']
But how to return values from column 1 and 3?
['607842':'4406', '607844':'7643', '607846':'2252', '607856':'7588']
I also tried this but I could not get what I wanted there either.
##import csv
##with open('C:/Data/Library/Peter/123.csv') as f:
## reader = csv.DictReader(f)
## for row in reader:
## print row
'''
{'Status': 'Retire', 'TYPE': '3', 'book_ref': '2397', 'book_id': '607838'}
{'Status': 'Keep', 'TYPE': '12', 'book_ref': '9644', 'book_id': '607839'}
{'Status': 'Retire', 'TYPE': '4', 'book_ref': '9295', 'book_id': '607841'}
{'Status': 'Keep', 'TYPE': '3', 'book_ref': '4406', 'book_id': '607842'}
{'Status': 'Retire', 'TYPE': '4', 'book_ref': '1798', 'book_id': '607843'}
{'Status': 'Keep', 'TYPE': '4', 'book_ref': '7643', 'book_id': '607844'}
{'Status': 'Retire', 'TYPE': '3', 'book_ref': '6778', 'book_id': '607845'}
{'Status': 'Keep', 'TYPE': '3', 'book_ref': '2252', 'book_id': '607846'}
{'Status': 'Retire', 'TYPE': '4', 'book_ref': '7910', 'book_id': '607855'}
{'Status': 'Keep', 'TYPE': '3', 'book_ref': '7588', 'book_id': '607856'}
解决方案
Or try this:
import csv
result={}
with open('C:/Temp/123.csv') as f:
reader = csv.DictReader(f)
for row in reader:
if row['Status']=='Keep':
result.update({row['book_id']:row['book_ref']})
which will produce:
{'607842': '9295', '607844': '7643', '607856': '7588'}
这篇关于将多个CSV列的值返回到Python字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文