从多个CSV列返回值到Python字典?
我试图在下面的Python字典中调用其他CSV列 该示例可以很好地返回一组值(列),但我需要为不相邻的两列返回值从多个CSV列返回值到Python字典?,python,csv,dictionary,Python,Csv,Dictionary,我试图在下面的Python字典中调用其他CSV列 该示例可以很好地返回一组值(列),但我需要为不相邻的两列返回值 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
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
正确返回第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 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'}
您可以这样简单地尝试:
with open("123.csv") as f:
my_list = []
next(f)
for x in f:
x = x.strip().split()
my_list.append((x[0],x[2]))
您可以这样简单地尝试:
with open("123.csv") as f:
my_list = []
next(f)
for x in f:
x = x.strip().split()
my_list.append((x[0],x[2]))
试试这个:
zipList = zip(*allrows[1:])
print dict(zip(zipList[0], zipList[2]))
输出:
{'607842': '9295', '607856': '7588', '607844': '7643', '607846': '2252'}
试试这个:
zipList = zip(*allrows[1:])
print dict(zip(zipList[0], zipList[2]))
输出:
{'607842': '9295', '607856': '7588', '607844': '7643', '607846': '2252'}
或者试试这个:
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']})
这将产生:
{'607842': '9295', '607844': '7643', '607856': '7588'}
或者试试这个:
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']})
这将产生:
{'607842': '9295', '607844': '7643', '607856': '7588'}
您是否尝试过csv.DictReader([)。通过正确的映射,我认为这将解决您的问题。是的,但不是。(您的快捷方式也返回404)您是否尝试过csv.DictReader([)。通过正确的映射,我认为这将解决您的问题。是的,但不是。(您的快捷方式也返回404)谢谢你对我有用。下面的其他回复很感谢,但不满足“状态”要求。谢谢你对我有用。下面的其他回复很感谢,但不满足“状态”要求。