我正在尝试使用以下数据创建字典:
ListA = ['Name', 'Age', 'Gender']
ListB = ['Alex', '22', 'Male']
['Kelly','21', 'Female']
Run Code Online (Sandbox Code Playgroud)
ListB来自FileB,看起来像这样:
Alex 22 Male,Kelly 21 Female (值选项卡分隔,组逗号分隔)
预期产量:
{'Name' : 'Alex', 'Age' : '22', 'Gender' : 'Male',
'Name' : 'Kelly', 'Age': '21', 'Gender' : 'Female'
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下代码:
fileB = glob.glob(filename + '.txt')
dfun = {}
ListB = []
for f in fileB:
Lines = open(f, 'r').read().split(',')
for i in Lines:
Lines2 = i.split('\t')
ListB.append(Lines2)
print(ListB) # this gives me ListB in the format above.
for i in …Run Code Online (Sandbox Code Playgroud)