I have a csv file
col1, col2, col3
1, 2, 3
4, 5, 6
I want to create a list of dictionary from this csv.
output as :
a= [{'col1':1, 'col2':2, 'col3':3}, {'col1':4, 'col2':5, 'col3':6}]
How can I do this?
Use csv.DictReader:
import csv
with open('test.csv') as f:
a = [{k: int(v) for k, v in row.items()}
for row in csv.DictReader(f, skipinitialspace=True)]
Will result in :
[{'col2': 2, 'col3': 3, 'col1': 1}, {'col2': 5, 'col3': 6, 'col1': 4}]
Another simpler answer:
import csv
with open("configure_column_mapping_logic.csv", "r") as f:
reader = csv.DictReader(f)
a = list(reader)
print a
Using the csv module and a list comprehension:
import csv
with open('foo.csv') as f:
reader = csv.reader(f, skipinitialspace=True)
header = next(reader)
a = [dict(zip(header, map(int, row))) for row in reader]
print a
Output:
[{'col3': 3, 'col2': 2, 'col1': 1}, {'col3': 6, 'col2': 5, 'col1': 4}]
Answering here after long time as I don't see any updated/relevant answers.
df = pd.read_csv('Your csv file path')
data = df.to_dict('records')
print( data )
# similar solution via namedtuple:
import csv
from collections import namedtuple
with open('foo.csv') as f:
fh = csv.reader(open(f, "rU"), delimiter=',', dialect=csv.excel_tab)
headers = fh.next()
Row = namedtuple('Row', headers)
list_of_dicts = [Row._make(i)._asdict() for i in fh]
Well, while other people were out doing it the smart way, I implemented it naively. I suppose my approach has the benefit of not needing any external modules, although it will probably fail with weird configurations of values. Here it is just for reference:
a = []
with open("csv.txt") as myfile:
firstline = True
for line in myfile:
if firstline:
mykeys = "".join(line.split()).split(',')
firstline = False
else:
values = "".join(line.split()).split(',')
a.append({mykeys[n]:values[n] for n in range(0,len(mykeys))})
Simple method to parse CSV into list of dictionaries
with open('/home/mitul/Desktop/OPENEBS/test.csv', 'rb') as infile:
header = infile.readline().split(",")
for line in infile:
fields = line.split(",")
entry = {}
for i,value in enumerate(fields):
entry[header[i].strip()] = value.strip()
data.append(entry)
Related
I have a dictionary in python in this way
my_dict = {'1':['a','b','c'], '2':['d','e','f']}
and i want to write a csv file in which it is displayed as it follows
1, a b c
2, d e f
because it is parsed by another application in this specific format.
Is there any way to it?
This is a way to do it:
my_dict = {'1':['a','b','c'], '2':['d','e','f']}
with open('data.csv', 'w') as f:
f.write('\n'.join([("%s, %s" % (k,' '.join(my_dict[k]))) for k in my_dict])
my_dict = {'1':['a','b','c'], '2':['d','e','f']}
from operator import itemgetter
import csv
with open('data.csv','w') as f:
a = csv.writer(f, delimiter = ',', lineterminator='\n')
for k,v in sorted(my_dict.items(), key=itemgetter(0)):
a.writerow([k,' ' + ' '.join(v)])
data.csv
1, a b c
2, d e f
I want to do the below in python.The csv file is:
item1,item2,item2,item3
item2,item3,item4,item1
i want to make a dictionary with unique keys item1, item2, item3 and item4.
dictionary = {item1: value1, item2: value2....}. Value is how many times the key appears in csv file.How can I do this?
Obtain a list of all items from your cvs:
with open('your.csv') as csv:
content = csv.readlines()
items = ','.join(content).split(',')
Then start the mapping
mapping = {}
for item in items:
mapping[item] = (mapping.get(item) or 0) + 1
and your will get the following:
>>> mapping
{'item2': 3, 'item3': 2, 'item1': 2, 'item4': 1}
import csv
from collections import Counter
# define a generator, that will yield you field after field
# ignoring newlines:
def iter_fields(filename):
with open(filename, 'rb') as f:
reader = csv.reader(f)
for row in reader:
for field in row:
yield field
# now use collections.Counter to count your values:
counts = Counter(iter_fields('stackoverflow.csv'))
print counts
# output:
# Counter({'item3': 2, 'item2': 2, 'item1': 1,
# ' item1': 1, ' item2': 1, 'item4': 1})
see https://docs.python.org/2/library/collections.html#collections.Counter
import csv
temp = dict()
with open('stackoverflow.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
for x in row:
if x in temp.keys():
temp[x] = int(temp[x]) + 1
else:
temp[x] = 1
print temp
The output is like:-
{'item2': 3, 'item3': 2, 'item1': 2, 'item4': 1}
I would like to write a Python dictionary inside a CSV file.
My code is:
import csv
cluster = {}
cluster['cluster0'] = [0,'value1','value2','value3']
cluster['cluster1'] = [1,'value1','value2','value3']
csvfile2 = "//home/tom/Desktop/cluster.csv"
with open(csvfile2, "w") as output:
writer = csv.writer(output, lineterminator='\n')
writer.writerows(cluster)
But instead of getting:
0,value1,value2,value3
1,value1,value2,value3
I have inside my CSV file:
c,l,u,s,t,e,r,0
c,l,u,s,t,e,r,1
Any suggestion please?
Instead of the dictionary name, you should call the .values() method
with open(csvfile2, "w") as output:
writer = csv.writer(output, lineterminator='\n')
writer.writerows(cluster.values())
As an example:
d = {1: [1,2,3], 2: [4,5,6]}
>>> d.keys()
[1, 2]
>>> d.values()
[[1, 2, 3], [4, 5, 6]]
I am trying to make a dictionary from a csv file in python, but I have multiple categories. I want the keys to be the ID numbers, and the values to be the name of the items. Here is the text file:
"ID#","name","quantity","price"
"1","hello kitty","4","9999"
"2","rilakkuma","3","999"
"3","keroppi","5","1000"
"4","korilakkuma","6","699"
and this is what I have so far:
txt = open("hk.txt","rU")
file_data = txt.read()
lst = [] #first make a list, and then convert it into a dictionary.
for key in file_data:
k = key.split(",")
lst.append((k[0],k[1]))
dic = dict(lst)
print(dic)
This just prints an empty list though. I want the keys to be the ID#, and then the values will be the names of the products. I will make another dictionary with the names as the keys and the ID#'s as the values, but I think it will be the same thing but the other way around.
Use the csv module to handle your data; it'll remove the quoting and handle the splitting:
results = {}
with open('hk.txt', 'r', newline='') as txt:
reader = csv.reader(txt)
next(reader, None) # skip the header line
for row in reader:
results[row[0]] = row[1]
For your sample input, this produces:
{'4': 'korilakkuma', '1': 'hello kitty', '3': 'keroppi', '2': 'rilakkuma'}
You can use csv DictReader:
import csv
result={}
with open('/tmp/test.csv', 'r', newline='') as f:
for d in csv.DictReader(f):
result[d['ID#']]=d['name']
print(result)
# {'1': 'hello kitty', '3': 'keroppi', '2': 'rilakkuma', '4': 'korilakkuma'}
You can use a dictionary directly:
dictionary = {}
file_data.readline() # skip the first line
for key in file_data:
key = key.replace('"', '').strip()
k = key.split(",")
dictionary[k[0]] = k[1]
try this or use any library to read the file.
txt = open("hk.txt","rU")
file_data = txt.read()
file_lines = file_data.split("\n")
lst = [] #first make a list, and then convert it into a dictionary.
for linenumber in range(1,len(file_lines)):
k = file_lines[linenumber].split(",")
lst.append((k[0][1:len(k[0])-1],k[1][1:len(k[1])-1]))
dic = dict(lst)
print(dic)
but you can use the dict directly as well.
I am trying to compare values of a particular column between 2 csv. I tried the following code for the same. However, I am not getting any output and no error too. Please help me with this
with open("File1.csv", "rb") as in_file1, open("File2.csv", "rb") as in_file2,open("File3.csv", "wb") as out_file:
reader1 = csv.reader(in_file1)
reader2 = csv.reader(in_file2)
writer = csv.writer(out_file)
for row2 in reader2:
for row1 in reader1:
if row2[0] == row1[0]:
row2[1] = row1[1]
writer.writerow(row2)
Here is how the data looks like:
File 1
A 100
B 200
C 300
D 400
E 500
FIle 2
A
C
E
E
E
D
File 3 (Should be)
A 100
C 300
E 500
E 500
E 500
D 400
File1.csv is a mapping. Read it first and store it in a dictionary. Then iterate over File2.csv and write it to File3.csv together with the value retrieved from the mapping dictionary.
The following code works for your example:
with open("File1.csv", "rb") as in_file1:
d = dict(csv.reader(in_file1, delimiter=' '))
with open("File2.csv", "rb") as in_file2, open("File3.csv", "wb") as out_file:
writer = csv.writer(out_file, delimiter=' ')
for rec in csv.reader(in_file2, delimiter=' '):
writer.writerow((rec[0], d[rec[0]]))
Just for an illustration, d looks like this:
{'A': '100', 'B': '200', 'C': '300', 'D': '400', 'E': '500'}
The values are strings (not integers), but this is not a problem, since we are just printing them into a file.
Why not simply use it this way:
lookup = {}
with open('file1', 'r') as f:
lookup = dict([l.split() for l in f.read().split('\n') if len(l) > 0])
with open('file2', 'r') as file2, open('out', 'w') as out:
for line in file2.readlines():
line = line.strip()
out.write("%s %s\n" % (line, lookup[line]))
I don't see a point using csv here