这是我的代码
sample_fh = dir + "sampleManifest.txt"
kids = {}
fid = {}
parents = {}
status = {}
sex = {}
with open(sample_fh) as f:
for line in f:
line = line.rstrip('\n')
row = line.split('\t')
fid = row[0]
iid = row[1]
relation = row[5]
status = row[6]
sex = row[7]
if relation != "Mother" and relation != "Father":
kids[iid] = 1
status[iid] = status
fid[iid] = fid
sex[iid]= row[7]
if relation == "Mother" or relation == "Father":
parents[(fid,relation)] = iid
我收到这个错误:
status[iid] = status
TypeError: 'str' object does not support item assignment
不确定发生了什么。以前的论坛说这个错误是在您更改字符串时引起的,但我很确定我没有更改任何字符串。
请您参考如下方法:
您在代码中重新分配 status status = row[6] 因此它不再是一个字典,或者为您的字典使用另一个名称或在循环中更改状态只是不要同时使用它。
status = {} # starts as a dict
status = row[6] # now the name status points to something else i.e a str
