是否可以将csv文件中的数据读取到字典中,使得第一列为键,第二列为键。
例如我有一个csv文件
code msg
123456 Lorem ipsum dolor sit amet, consectetur adipiscing elit
345981 sed do eiusmo ut labore, et dolore magna aliqua;
459827 ullamco, laboris nisi ut aliquip ex ea commodo consequat.
490023 veniam, quis nostrud exercitation
345612 mollit anim id est laborum.
code表示键,而msg表示与每个代码关联的值。
import csv
with open('test.csv') as f:
reader = csv.reader(f)
mydict = {rows[0]:rows[1:] for rows in reader}
print(mydict)
x = mydict.get("123456")
print(x)
结果:
{'code;msg': [], '123456;Lorem ipsum dolor sit amet': [' consectetur adipiscing elit'], '345981;"sed do eiusmo ut labore': [' et dolore magna aliqua;"'], '459827;ullamco': [' laboris nisi ut aliquip ex ea commodo consequat.'], '490023;veniam': [' quis nostrud exercitation'], '345612;mollit anim id est laborum.': []}
None
我想搜索与每个键相关的值。
EG:我写的时候:
key= "123456"
value=mydict.get(key)
print(key + "has this value : " + value)
我会得到一个输出:
>>> The key 123456 has this value :Lorem ipsum dolor sit amet, consectetur adipiscing elit
答案 0 :(得分:2)
没有任何导入,您可以使用:
with open('test.csv') as f:
csv = f.readlines()
d = {}
for line in csv[1:]: # Loop csv lines skipping first line csv[1:] (headers)
m = line.split()
if len(m) > 1:
d[m[0]] = " ".join(m[1:])
print(d)
输出:
{'123456': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', '345981': 'sed do eiusmo ut labore, et dolore magna aliqua;', '459827': 'ullamco, laboris nisi ut aliquip ex ea commodo consequat.', '490023': 'veniam, quis nostrud exercitation', '345612': 'mollit anim id est laborum.'}
注意:
通常要使用key
进行搜索:
if '123456' in d:
print(d['123456'])
# Lorem ipsum dolor sit amet, consectetur adipiscing elit
打印字典keys
和values
print(d.keys(), d.values())
# dict_keys(['123456', '345981', '459827', '490023', '345612'])
# dict_values(['Lorem ipsum dolor sit amet, consectetur adipiscing elit', 'sed do eiusmo ut labore, et dolore magna aliqua;', 'ullamco, laboris nisi ut aliquip ex ea commodo consequat.', 'veniam, quis nostrud exercitation', 'mollit anim id est laborum.'])
答案 1 :(得分:1)
问题在于输入数据,文件包含多个逗号,并且您正在使用csv阅读器阅读。第二列应该用双引号引起来。
code,msg
"123456","Lorem ipsum dolor sit amet, consectetur adipiscing elit"
"345981","sed do eiusmo ut labore, et dolore magna aliqua;"
"459827","ullamco, laboris nisi ut aliquip ex ea commodo consequat."
"490023","veniam, quis nostrud exercitation"
"345612","mollit anim id est laborum."
在修改数据后,如果您执行代码段,则可以正常工作。
{'code': ['msg'], '123456': ['Lorem ipsum dolor sit amet, consectetur adipiscing elit'], '345981': ['sed do eiusmo ut labore, et dolore magna aliqua;'], '459827': ['ullamco, laboris nisi ut aliquip ex ea commodo consequat.'], '490023': ['veniam, quis nostrud exercitation'], '345612': ['mollit anim id est laborum.']}
['Lorem ipsum dolor sit amet, consectetur adipiscing elit']
您可以在pandas中使用todic方法,使用pandas读取csv文件,然后转换为数据框,然后执行以下代码
df.set_index('code').T.to_dict('list')
完整代码:
import pandas as pd
df = pd.read_csv(filepath_or_buffer = CSV_FILE_PATH")
df.set_index('code').T.to_dict('list')
输出:
{123456: ['Lorem ipsum dolor sit amet, consectetur adipiscing elit'],
345981: ['sed do eiusmo ut labore, et dolore magna aliqua;'],
459827: ['ullamco, laboris nisi ut aliquip ex ea commodo consequat.'],
490023: ['veniam, quis nostrud exercitation'],
345612: ['mollit anim id est laborum.']}
答案 2 :(得分:1)
使用csv
模块。
例如:
import csv
result = {}
with open('test.csv') as infile:
reader = csv.reader(infile, delimiter=';')
next(reader) #Skip Header
for row in reader: #Iterate Each Line
result[row[0]] = row[1] #Form Dictionary
print(result)
输出:
{'123456': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
'345612': 'mollit anim id est laborum.',
'345981': 'sed do eiusmo ut labore, et dolore magna aliqua',
'459827': 'ullamco, laboris nisi ut aliquip ex ea commodo consequat.',
'490023': 'veniam, quis nostrud exercitation'}