I've created a nested dictionary from a file as so:
import csv
d = {}
with open("file.txt", 'r') as f:
data = csv.DictReader(f, delimiter="\t")
for row in data:
item = d.get(row["value"], dict())
item[row["value"]] = row["need"]
d[row["item"]] = item
print d
However, there should be multiple entries for every item. For example, here is what the output from this code looks like:
d = {'key1': {'153908482': 'ctaggaacca'}, 'key2': {'115057116': 'gtaattctga'}, 'key3': {'133381371': 'ctgaaaagat'}
The dictionary should look something like this:
d = {'key1': {'153908482': 'ctaggaacca', '143543': 'atccgatcgg'}, 'key2': {'115057116': 'gtaattctga', '12321333': 'accccgta'}, 'key3': {'133381371': 'ctgaaaagat'}
I'm losing some information in the creation of my dictionary. Any help would be greatly appreciated.
The problem is that you are assigning item
to a different key than you get from the dict.
item = d.get(row["value"], dict()) <-- you never assign to row["value"] so
this always returns an empty dict
item[row["value"]] = row["need"]
d[row["item"]] = item <-- since you didn't get the existing
row["item"], you keep overwriting
them
I reworked your script a bit so that it is runnable with test data but the only real change is grabbing data from correct key
import csv
from cStringIO import StringIO
f = StringIO("""item value need
key1 153908482 ctaggaacca
key2 115057116 gtaattctga
key3 133381371 ctgaaaagat
key1 143543 atccgatcgg
key2 212321333 accccgta
""")
d = {}
#with open("file.txt", 'r') as f:
data = csv.DictReader(f, delimiter="\t")
for row in data:
# item = d.get(row["value"], dict())
item = d.get(row["item"], dict())
item[row["value"]] = row["need"]
d[row["item"]] = item
print d
And here's an alternate implementation using defaultdict
which creates the inner dict for you
import collections
d = collections.defaultdict(dict)
data = csv.DictReader(f, delimiter="\t")
for row in data:
d[row["item"]][row["value"]] = row["need"]
print d
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.