My text file is a large list of data (by large meaning I can't format it by hand) consisting of only numbers and is formatted like this:
1 5555 6666
2 5555 6666
1 7755 6666
3 8888 6666
I would like to use the first two columns as my keys and the third remaining column as their value.
Here is my code:
import string
def load (filename):
with open ('filename', 'r'):
dict = {}
for line in file.readline():
key, site, value = dict([line.strip().split('\t')for line in file
dict[key[0]]+[site[1]]= value[2]
return dict
However my code fails.
My desired output is this:
{('1', '5555'): '6666', ('2', '5555'): '6666', ('1', '7755'): '6666', ('3', '8888'): '6666'}
Is it possible to achieve my output? Am I on the right track? If not, where did I go wrong and how can I fix it?
Thank you
You can use the csv module to read the content splitting the elements by whatever delimiter you pass then unpack and use the first two elements in a tuple as the key and the last as the value:
import csv
with open("in.csv") as f:
d = {}
r = csv.reader(f, delimiter=" ") # pass whatever your delimiter is
for row in r: # first row 1 5555 6666 -> ["1", "5555", "6666"]
a, b, v = row # a,b,c = "1", "5555", "6666"
d[(a, b)] = v # create a tuple from the first two elements of the row
print(d)
{('3', '8888'): '6666', ('1', '5555'): '6666', ('1', '7755'): '6666', ('2', '5555'): '6666'}
If you want the data ordered use an OrderedDict:
import csv
from collections import OrderedDict
with open("in.csv") as f:
d = OrderedDict()
r = csv.reader(f, delimiter=" ")
for row in r:
a, b, v = row
d[(a, b)] = v
print(d)
If you had a chance the the keys could repeat then you would need to store the values in a list or some container:
import csv
from collections import OrderedDict
with open("in.csv") as f:
d = OrderedDict()
r = csv.reader(f, delimiter=" ")
for row in r:
a, b, v = row
d.setdefault((a,b),[]).append(v)
print(d)
You own code has multiple mistakes:
def load(filename):
with open(filename, 'r') as f: # as f and pass variable filename not a string
d = {} # don't shadow the python dict
for line in f: # iterate over the file object
key, site, value = line.split() # unpack
d[(key, site)] = value # already unpacked so just use the variables
return d
Then call your function passing the filename:
print(load("in.csv"))
{('1', '5555'): '6666', ('3', '8888'): '6666', ('2', '5555'): '6666', ('1', '7755'): '66`66'}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments