I'm migrating from Perl to Python and unable to identify the equivalent
of key of key concept. The following codes run well,
with open('test.txt','rb') as tsvin:
tsvin = csv.reader(tsvin, delimiter='\t')
ID = row[1]
attr[ID]['adm3'] = row[2]
still doesn't work. Some posts suggest using module dict but some do
not. I'm a bit confused now. Any suggestions?
Python doesn't have Perl's autovivication feature. If you want the
value to be a dict then you need to create that dict first:
attr[ID] = {}
attr[ID]['adm3'] = row[2]
You could also have a look at the 'defaultdict' class in the
'collections' module.
I identify the information below:
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
While it is fine for a small dataset, I need a more generic way to do so.
Indeed the "test.txt" in my example contains more columns of attributes
like:
ID address age gender phone-number race education ...
ABC123 Ohio, USA 18 F 800-123-456 european university
ACC499 London 33 M 800-111-400 african university
...
so later I can retrieve the information in python by:
attr['ABC123'].address (containing 'Ohio, USA')
attr['ABC123'].race (containing 'european')
attr['ACC499'].age (containing '33')
Using a csv.DictReader comes close with minimal effort:
# write demo data to make the example self-contained
with open("tmp.csv", "w") as f:
f.write("""\
ID,address,age,gender,phone-number,race,education
ABC123,"Ohio, USA",18,F,800-123-456,european,university
ACC499,London,33,M,800-111-400,african,university
""")
import csv
import pprint
with open("tmp.csv") as f:
attr = {row["ID"]: row for row in csv.DictReader(f)}
pprint.pprint(attr)
print(attr["ACC499"]["age"])
The "dict comprehension"
attr = {row["ID"]: row for row in csv.DictReader(f)}
is a shortcut for
attr = {}
for row in csv.DictReader(f):
attr[row["ID"]] = row
If you insist on attribute access (row.age instead of row["age"]) you can
use a namedtuple. This is a bit more involved:
import csv
import pprint
from collections import namedtuple
with open("tmp.csv") as f:
rows = csv.reader(f)
header = next(rows)
# make sure column names are valid Python identifiers
header = [column.replace("-", "_") for column in header]
RowType = namedtuple("RowType", header)
key_index = header.index("ID")
attr = {row[key_index]: RowType(*row) for row in rows}
pprint.pprint(attr)
print(attr["ABC123"].race)
The following links mention something similar,
Too many, so I checked none of them