Python Dictionary to a specific format in .csv - python

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

Related

Does anyone know how to get info from a csv?

Im trying to collect info from a csv file and turn the row values into variables and carry those variables into different def functions finding such variables such as the max of whats inside and basically gather information from the csv.
Simple, you can use the csv module
for example, have the following csv file
a,1
b,7
c,5
d,2
e,6
>>> import csv
>>> filename = "/Users/sunnky/Desktop/test.csv"
>>> d = {}
>>> with open(filename, mode='r', encoding='utf-8-sig') as f:
... reader = csv.reader(f)
... for k, v in reader:
... d[k] = v
...
>>> new_d = sorted(d.items(), key = lambda m: m[1])
>>> min, max = new_d[0], new_d[len(new_d)-1]
>>> print(min, max)
('a', '1') ('b', '7')

Joining CSV fields

I need to convert
Name | Org
a | 5
a | 6
b | 5
c | 7
into
Name | Org
a | 5,6
b | 5
c | 7
my first attempt was with this code
while i < len(nameColumn):
if nameColumn[i] not in resultC1:
resultC1.append(nameColumn[i])
while l < len(nameColumn):
if nameColumn[l] == nameColumn[i]:
tempdata += organizationColumn[l] + ','
l += 1
resultC2.append(tempdata[:-1])
tempdata = ''
k += 1
i += 1
which just ends up with the result
Name | Org
a |
b |
c |
Any help would be greatly appreciated. I havent had luck finding anything on this yet. I'm reading the data from a .CSV file into lists and working with that data and storing results into resultC1 and resultC2
Here's a solution using collections.OrderedDict:
import csv
from collections import OrderedDict
data = OrderedDict()
with open('test.csv') as f:
reader = csv.reader(f)
for i, line in enumerate(reader):
if i == 0:
continue
if line[0] not in data:
data[line[0]] = []
data[line[0]].append(line[1])
for k, v in data.items():
print(k, '|', ', '.join(v))
An OrderedDict retains its order. The keys are the Names, and the values are lists of all Orgs associated with it.
Output:
a | 5, 6
b | 5
c | 7
If your csv has different delimiters from comma, then you'll have to specify that delimiter. I've assumed commas in my example.
Here's a much simpler solution with pandas:
In [443]: df.head()
Out[443]:
Name Org
0 a 5
1 a 6
2 b 5
3 c 7
In [445]: for k, v in df.groupby('Name').apply(lambda x: list(x['Org'])).iteritems():
...: print(k, '|', ', '.join(map(str, v)))
...:
a | 5, 6
b | 5
c | 7
Use OrderedDict calling setdefault with an empty list, and the csv module:
import csv
from collections import OrderedDict
organizations = OrderedDict()
with open(filename) as infile:
for name, org in csv.reader(infile, delimiter='|'):
organizations.setdefault(name, []).append(org)
Then you can write the dictionary:
with open(filename, 'w') as outfile:
writer = csv.writer(outfile, delimiter='|')
for name, orgs in organizations.items():
writer.writerow([name, ','.join(orgs)])
Assuming you're starting with the two arrays implied in your sample code, I'd go with something like this:
from collections import defaultdict
nameColumn = ['a', 'a', 'b', 'c']
organizationColumn = ["5", "6", "5", "7"]
merged = defaultdict(list)
for name, org in zip(nameColumn, organizationColumn):
merged[name].append(org)
for k, v in merged.items():
print(f'{k} | {v}'))
The solution using itertools.groupby() function:
import csv, itertools
with open('yourfile.csv', 'r') as f:
reader = csv.reader(f, delimiter='|', skipinitialspace=True)
head = next(reader) # header line
items = [list(g) for k,g in itertools.groupby(sorted(reader), key=lambda x: x[0])]
fmt = '{0[0]:<5} | {0[1]:^5}' # format spec
print(fmt.format(head))
for item in items:
print(fmt.format([item[0][0], ','.join(i[1] for i in item)] if len(item) > 1 else item[0]))
The output:
Name | Org
a | 5,6
b | 5
c | 7
Here is another solution that can be generic to have delimiter for input and output file.
def parseData(fileName, delimiter):
dictionary={}
with open(fileName, 'r') as iFile:
for line in iFile.readlines():
row = line.split(delimiter)
values = []
if (row[0] in dictionary.keys()):
values = dictionary[row[0]]
values.append(row[1].replace('\n',''))
else:
values.append(row[1].replace('\n',''))
dictionary[row[0]] = values
dictionary[row[0]] = values
## print for debugging purpose
print(dictionary)
return dictionary
def writeData(fileName, odelimiter, idelimiter, dictionary):
with open(fileName, 'w') as oFile:
for key, values in dictionary.items():
data=""
for value in values:
data = data + value + idelimiter
data=data[:-1]
## print for debugging purpose
print(key, data)
oFile.write(key + odelimiter + data + "\n")
## main
dictionary=parseData('inputPipe.txt', "|")
writeData('output.txt', "|", ",", dictionary)
inputPipe.txt
a|5
a|6
b|5
c|7
output.txt
a|5,6
b|5
c|7
Sample Run
{'a': ['5', '6'], 'b': ['5'], 'c': ['7']}
a 5,6
b 5
c 7

File Columns to Dictionary

Hello I have a file comprising 4 columns,
a 1 45 test
b 2 42 test
c 3 64 test
I wish to read this file to a dictionary such that column 3 is the key and column 1 is the value, i.e.,
d = {45:'a', 42:'b', 64:'c'}
Keep it simple:
>>>
>>> d = dict()
>>> with open('test.txt') as f:
for line in f:
val, foo, key, bar = line.split()
d[key] = val
>>> d
{'64': 'c', '45': 'a', '42': 'b'}
>>>
Use the csv module to parse the file. Change the delimiter parameter to whatever is the delimiter in your input file. I have assumed it to be tabs.
import csv
d = {}
with open('your-input-file', 'r') as input_file:
csv_reader = csv.reader(input_file, delimiter='\t')
for row in csv_reader:
d[row[2]] = row[0]
input_file.close()

convert csv file to list of dictionaries

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)

Comparing values between 2 CSV files and writing to a 3rd CSV file

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

Categories

Resources