List to dictionary - improvement? - python

The following code does what I want, but any other way more python-style of doing it?
Having file in the format:
key1:value1,key2:value2,...
key21:value21,key22:value22,...
.
EOF
and code:
file = open(fileName, 'r')
for lines in file:
line = lines.split(",")
my_dict = {}
for item in line:
key_value = item.split(":")
my_dict.update({key_value[0]:key_value[1]})
Thanks

A faster & more pythonic way would be to use csv module (comma separated by default) and split items in a double flattened generator comprehension fed to dict that accepts tuples with 2 elements:
import csv
with open("test.csv",newline="") as f: # replace ,newline="" by ,"rb" in python 2
cr = csv.reader(f)
d = dict(x.split(":") for row in cr for x in row)
print(d)
result:
{'key1': 'value1', 'key22': 'value22', 'key21': 'value21', 'key2': 'value2'}
non-csv version:
import csv
with open("test.csv") as f:
d = dict(x.split(":") for line in f for x in line.split(","))

Using split():
list.txt:
key1:value1,key2:value2,key3:value3
key21:value21,key22:value22
Hence:
with open("list.txt") as fileObj:
content = fileObj.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
for elem in line.split(","):
print({elem .split(":")[0] : elem.split(":")[1]})
OUTPUT:
{'key1': 'value1'}
{'key2': 'value2'}
{'key3': 'value3'}
{'key21': 'value21'}
{'key22': 'value22'}
OR
If you want them stored in the dict:
for line in content:
for x in line.split(","):
dict_.update({x.split(":")[0] : x.split(":")[1]})
print(dict_['key1']) # value1

Related

How can I read from this file and turn it to a dictionary?

I've tried several methods to read from this file and turn it to a dictionary but I'm having a lot of errors
I've tried the following method but it did not work I got not enough values unpack.
d = {}
with open("file.txt") as f:
for line in f:
(key, val) = line.split()
d[int(key)] = val
I want to read and convert it to this:
{123: ['Ahmed Rashed', 'a', '1000.0'], 456: ['Noof Khaled', 'c', '0.0'], 777: ['Ali Mahmood', 'a', '4500.0']}
Split on commas instead.
d = {}
with open("file.txt") as f:
for line in f:
parts = line.rstrip('\n').split(',')
d[int(parts[0])] = parts[1:]
Using csv.reader to read the file and split it into its fields:
import csv
with open("file.txt") as f:
d = {
int(num): data
for num, *data in csv.reader(f)
}

Convert multiple line on a file to tuple & lists format

I am trying to covert the multiple lines under a file, to below format (tuples + list), but still struggling
sample lines under file
USER1.TEST1,SCHEMA2.TEST2
USER5.TEST,USER3.TEST1,RATE=100
SCHEMA5.TEST5,CORE12.TEST3,RATE=500
Expected output
[('USER','TEST1','USER','TEST2'),('USER5','TEST','USER3','TEST1','RATE=100'),('SCHEMA5','TEST5','CORE12','TEST3','RATE=500')]
Code i am trying ...
o_list = []
with open (i_list,'rb') as f:
if not 'tab' in i_list:
r = csv.reader(f)
else:
for line in f.readlines():
f, s = line.strip().split('.')
s = s.split(',')
o_list.append((f,) + tuple(s))
return o_list
Try using a one-liner list comprehension with a re.split:
import re
with open('filename.txt', 'r') as f:
print([re.split('\.|,', i.rstrip()) for i in f])
Output:
[['USER1', 'TEST1', 'SCHEMA2', 'TEST2'], ['USER5', 'TEST', 'USER3', 'TEST1', 'RATE=100'], ['SCHEMA5', 'TEST5', 'CORE12', 'TEST3', 'RATE=500']]
It seems like as long as you are using the csv module, you should leverage the fact that it will split on a delimiter of your choice. So you can split on , and then on each line split the fields if you need to. You can use itertools.chain to flatten the sublists into tuples:
from itertools import chain
with open ('test.csv') as f:
if not 'tab' in i_list: # not sure what i_list is
r = csv.reader(f)
else:
r = csv.reader(f, delimiter=',') # split on `,` first
o_list = [tuple(chain.from_iterable(s.split('.') for s in line)) for line in r]
o_list:
[('USER1', 'TEST1', 'SCHEMA2', 'TEST2'),
('USER5', 'TEST', 'USER3', 'TEST1', 'RATE=100'),
('SCHEMA5', 'TEST5', 'CORE12', 'TEST3', 'RATE=500')]

How to create a dictionary from a txt file on Python

This is the txt file content I have:
salesUnits:500
priceUnit:11
fixedCosts:2500
variableCostUnit:2
I need to create a dictionary in Python that will read the file and make the keys the salesUnits etc. and the values the numbers. The code I have so far will only print the variable cost per unit:
with open("myInputFile.txt") as f:
content = f.readlines()
myDict = {}
for line in content:
myDict=line.rstrip('\n').split(":")
print(myDict)
How can I fix the code so that all key and value pairs show up? Thank you!
You're overwriting myDict each time you call myDict=line.rstrip('\n').split(":"). The pattern to add to a dictionary is dictionary[key] = value.
myDict = {}
with open("myInputFile.txt") as f:
for line in f:
key_value = line.rstrip('\n').split(":")
if len(key_value) == 2:
myDict[key_value[0]]=key_value[1]
print(myDict)
outputs
{'fixedCosts': '2500', 'priceUnit': '11', 'variableCostUnit': '2', 'salesUnits': '500'}
Using a simple dict comprehension will handle this:
with open('testinput.txt', 'r') as infile:
dict = {
line.strip().split(':')[0]:
int(line.strip().split(':')[1])
if line.strip().split(':')[1].isdigit()
else
line.strip().split(':')[1]
for line in infile.readlines()}
print(dict)
Output:
{'salesUnits': 500, 'priceUnit': 11, 'fixedCosts': 2500, 'variableCostUnit': 2}
If you wish to bring the numbers in as simple strings, just use:
dict = {
line.strip().split(':')[0]:
line.strip().split(':')[1]
for line in infile.readlines()}
Note also that you can add handling for other data types or data formatting using additional variations of:
int(line.strip().split(':')[1])
if line.strip().split(':')[1].isdigit()
else
myDict = {}
with open('dict.txt', 'r') as file:
for line in file:
key, value = line.strip().split(':')
myDict[key] = value
print myDict
Output:
{'fixedCosts': '2500', 'priceUnit': '11', 'variableCostUnit': '2', 'salesUnits': '500'}

Write dictionary (keys and values) to a csv file

I just want the csv file to look like this:
key,item1,item2,item3
key2,itema,itemB,itemC
and so on
The dictionary has a key and the value is a list of floats.
This is the current code I have to write to the csv file but all it does is write out the key like this: k,e,y,s
Any help is appreciated
with open(outFileName1, 'w') as outfile:
csv_Writer = csv.writer(outfile)
csv_Writer.writerows(dict1)
import csv
dict_data = {'key1': [1, 2, 3], 'key2': [4, 5, 6]}
with open("dict2csv.txt", 'w') as outfile:
csv_writer = csv.writer(outfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for k,v in dict_data.items():
csv_writer.writerow([k] + v)
This code will write each key, value pair in your desire format on separate line in csv file.
Without getting into details how CSV works you can easily solve it with something like:
with open("out.txt", 'w') as outfile:
for k,v in dict1.items():
outfile.write(str(k))
for item in v:
outfile.write(","+str(item))
outfile.write(" ")
Your current code iterates the dictionary which yields keys only. Take a look at
import csv
data = {
'key1': ['item1', 'item2'],
'key2': ['item3', 'item4']
}
with open('', 'w') as outfile:
writer = csv.writer(outfile)
for k, v in data.iteritems():
writer.writerow([k] + v)
Notice that it iterates key-value pairs returned by .iteritems(). The key is inserted into a list which is concatenated with the value list.

How to turn CSV data into dictionary [duplicate]

This question already has answers here:
Convert .csv table to dictionary [duplicate]
(4 answers)
Closed 9 years ago.
I have a CSV file which I am opening through this code:
open(file,"r")
When I read the file I get the output:
['hello', 'hi', 'bye']
['jelly', 'belly', 'heli']
['red', 'black', 'blue']
I want the otput something like this:
{hello:['jelly','red'], hi:['belly','black'], 'bye':['heli','blue']}
but I have no idea how
You can use collections.defaultdict and csv.DictReader:
>>> import csv
>>> from collections import defaultdict
>>> with open('abc.csv') as f:
reader = csv.DictReader(f)
d = defaultdict(list)
for row in reader:
for k, v in row.items():
d[k].append(v)
...
>>> d
defaultdict(<type 'list'>,
{'hi': ['belly', 'black'],
'bye': ['heli', 'blue'],
'hello': ['jelly', 'red']})
csv = [
['hello', 'hi', 'bye'],
['jelly', 'belly', 'heli'],
['red', 'black', 'blue'],
]
csv = zip(*csv)
result = {}
for row in csv:
result[row[0]] = row[1:]
yourHash = {}
with open(yourFile, 'r') as inFile:
for line in inFile:
line = line.rstrip().split(',')
yourHash[line[0]] = line[1:]
This assumes that each key is unique to one line. If not, this would have to be modified to:
yourHash = {}
with open(yourFile, 'r') as inFile:
for line in inFile:
line = line.rstrip().split(',')
if line[0] in yourHash:
yourHash[line[0]] += line[1:]
else:
yourHash[line[0]] = line[1:]
Of course, you can use csv, but I figured that someone would definitely post that, so I gave an alternative way to do it. Good luck!
You can use csv, read the first line to get the header, create the number of lists corresponding to the header and then create the dict:
import csv
with open(ur_csv) as fin:
reader=csv.reader(fin, quotechar="'", skipinitialspace=True)
header=[[head] for head in next(reader)]
for row in reader:
for i, e in enumerate(row):
header[i].append(e)
data={l[0]:l[1:] for l in header}
print(data)
# {'hi': ['belly', 'black'], 'bye': ['heli', 'blue'], 'hello': ['jelly', 'red']}
If you want something more terse, you can use Jon Clements excellent solution:
with open(ur_csv) as fin:
csvin = csv.reader(fin, quotechar="'", skipinitialspace=True)
header = next(csvin, [])
data=dict(zip(header, zip(*csvin)))
# {'bye': ('heli', 'blue'), 'hello': ('jelly', 'red'), 'hi': ('belly', 'black')}
But that will produce a dictionary of tuples if that matters...
And if you csv file is huge, you may want to rewrite this to generate a dictionary row by row (similar to DictReader):
import csv
def key_gen(fn):
with open(fn) as fin:
reader=csv.reader(fin, quotechar="'", skipinitialspace=True)
header=next(reader, [])
for row in reader:
yield dict(zip(header, row))
for e in key_gen(ur_csv):
print(e)
# {'hi': 'belly', 'bye': 'heli', 'hello': 'jelly'}
{'hi': 'black', 'bye': 'blue', 'hello': 'red'} etc...

Categories