I am trying to create a dictionary from a csv file. The first column of the csv file contains unique keys and the second column contains values. Each row of the csv file represents a unique key, value pair within the dictionary. I tried to use the csv.DictReader
and csv.DictWriter
classes, but I could only figure out how to generate a new dictionary for each row. I want one dictionary. Here is the code I am trying to use:
我正在尝试从csv文件创建字典。csv文件的第一列包含唯一的键,第二列包含值。csv文件的每一行表示字典中的唯一键值对。我尝试使用csv。DictReader和csv。DictWriter类,但我只知道如何为每一行生成一个新字典。我想要一本字典。下面是我正在尝试使用的代码:
import csv
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
for rows in reader:
k = rows[0]
v = rows[1]
mydict = {k:v for k, v in rows}
print(mydict)
When I run the above code I get a ValueError: too many values to unpack (expected 2)
. How do I create one dictionary from a csv file? Thanks.
当我运行上面的代码时,我得到一个ValueError:太多的值需要解压(期望2)。如何从csv文件创建一个字典?谢谢。
10 个解决方案
#1
90
I believe the syntax you were looking for is as follows:
我想您要寻找的语法如下:
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = {rows[0]:rows[1] for rows in reader}
Alternately, for python <= 2.7.1, you want:
另外,对于python <= 2.7.1,您需要:
mydict = dict((rows[0],rows[1]) for rows in reader)
#2
35
import csv
reader = csv.reader(open('filename.csv', 'r'))
d = {}
for row in reader:
k, v = row
d[k] = v
#3
28
Open the file by calling open and then csv.DictReader
.
通过调用Open打开文件,然后调用csv.DictReader来打开文件。
input_file = csv.DictReader(open("coors.csv"))
You may iterate over the rows of the csv file dict reader object by iterating over input_file.
您可以通过迭代input_file来遍历csv文件dict reader对象的行。
for row in input_file:
print row
OR To access first line only
或者只能访问第一行
dictobj = csv.DictReader(open('coors.csv')).next()
#4
10
You have to just convert csv.reader to dict:
你只需要转换csv。读者对dict:
~ >> cat > 1.csv
key1, value1
key2, value2
key2, value22
key3, value3
~ >> cat > d.py
import csv
with open('1.csv') as f:
d = dict(filter(None, csv.reader(f)))
print(d)
~ >> python d.py
{'key3': ' value3', 'key2': ' value22', 'key1': ' value1'}
#5
10
You can also use numpy for this.
你也可以使用numpy。
from numpy import loadtxt
key_value = loadtxt("filename.csv", delimiter=",")
mydict = { k:v for k,v in key_value }
#6
4
This isn't elegant but a one line solution using pandas.
这不是优雅的,而是使用熊猫的单线解决方案。
import pandas as pd
pd.read_csv('coors.csv', header=None, index_col=0, squeeze=True).to_dict()
If you want to specify dtype for your index (it can't be specified in read_csv if you use the index_col argument because of a bug):
如果您想为索引指定dtype(如果由于错误而使用index_col参数,则不能在read_csv中指定dtype):
import pandas as pd
pd.read_csv('coors.csv', header=None, dtype={0: str}).set_index(0).squeeze().to_dict()
#7
3
I'd suggest adding if rows
in case there is an empty line at the end of the file
如果文件末尾有空行,我建议添加if行
import csv
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = dict(row[:2] for row in reader if row)
#8
3
If you are OK with using the numpy package, then you can do something like the following:
如果您对使用numpy包没有问题,那么您可以做以下事情:
import numpy as np
lines = np.genfromtxt("coors.csv", delimiter=",", dtype=None)
my_dict = dict()
for i in range(len(lines)):
my_dict[lines[i][0]] = lines[i][1]
#9
0
You can use this, it is pretty cool:
你可以用这个,很酷:
import dataconverters.commas as commas
filename = 'test.csv'
with open(filename) as f:
records, metadata = commas.parse(f)
for row in records:
print 'this is row in dictionary:'+rowenter code here
#10
0
One-liner solution
一行程序解决方案
import pandas as pd
dict = {row[0] : row[1] for _, row in pd.read_csv("file.csv").iterrows()}
#1
90
I believe the syntax you were looking for is as follows:
我想您要寻找的语法如下:
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = {rows[0]:rows[1] for rows in reader}
Alternately, for python <= 2.7.1, you want:
另外,对于python <= 2.7.1,您需要:
mydict = dict((rows[0],rows[1]) for rows in reader)
#2
35
import csv
reader = csv.reader(open('filename.csv', 'r'))
d = {}
for row in reader:
k, v = row
d[k] = v
#3
28
Open the file by calling open and then csv.DictReader
.
通过调用Open打开文件,然后调用csv.DictReader来打开文件。
input_file = csv.DictReader(open("coors.csv"))
You may iterate over the rows of the csv file dict reader object by iterating over input_file.
您可以通过迭代input_file来遍历csv文件dict reader对象的行。
for row in input_file:
print row
OR To access first line only
或者只能访问第一行
dictobj = csv.DictReader(open('coors.csv')).next()
#4
10
You have to just convert csv.reader to dict:
你只需要转换csv。读者对dict:
~ >> cat > 1.csv
key1, value1
key2, value2
key2, value22
key3, value3
~ >> cat > d.py
import csv
with open('1.csv') as f:
d = dict(filter(None, csv.reader(f)))
print(d)
~ >> python d.py
{'key3': ' value3', 'key2': ' value22', 'key1': ' value1'}
#5
10
You can also use numpy for this.
你也可以使用numpy。
from numpy import loadtxt
key_value = loadtxt("filename.csv", delimiter=",")
mydict = { k:v for k,v in key_value }
#6
4
This isn't elegant but a one line solution using pandas.
这不是优雅的,而是使用熊猫的单线解决方案。
import pandas as pd
pd.read_csv('coors.csv', header=None, index_col=0, squeeze=True).to_dict()
If you want to specify dtype for your index (it can't be specified in read_csv if you use the index_col argument because of a bug):
如果您想为索引指定dtype(如果由于错误而使用index_col参数,则不能在read_csv中指定dtype):
import pandas as pd
pd.read_csv('coors.csv', header=None, dtype={0: str}).set_index(0).squeeze().to_dict()
#7
3
I'd suggest adding if rows
in case there is an empty line at the end of the file
如果文件末尾有空行,我建议添加if行
import csv
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = dict(row[:2] for row in reader if row)
#8
3
If you are OK with using the numpy package, then you can do something like the following:
如果您对使用numpy包没有问题,那么您可以做以下事情:
import numpy as np
lines = np.genfromtxt("coors.csv", delimiter=",", dtype=None)
my_dict = dict()
for i in range(len(lines)):
my_dict[lines[i][0]] = lines[i][1]
#9
0
You can use this, it is pretty cool:
你可以用这个,很酷:
import dataconverters.commas as commas
filename = 'test.csv'
with open(filename) as f:
records, metadata = commas.parse(f)
for row in records:
print 'this is row in dictionary:'+rowenter code here
#10
0
One-liner solution
一行程序解决方案
import pandas as pd
dict = {row[0] : row[1] for _, row in pd.read_csv("file.csv").iterrows()}