我在文本文件中有下面提到的文本,每个键都在一个新行中。我需要转换为如下所述的 JSON 数据,请任何人帮助解决这个问题。示例文本文件name: Samuels heardpatient id : 7898received date: 06/22/2019firstname : scott deny dob : 06/22/2019必需的 JSON 数据{ "name": "Samuels heard", "patient id": "7898", "received date": "06/22/2019", "firstname": "scott deny", "dob": "06/22/2019"}
1 回答
蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
尝试这个
mydict = {}
with open("mytext.txt", "r") as f:
for line in f:
mylst = line.rstrip('\n').split(":")
mydict[mylst[0]] = mylst[1]
print(mydict)
输出:
{'name': ' Samuels heard', 'patient id ': ' 7898', 'received date': ' 06/22/2019', 'firstname ': ' scott deny', 'dob ': ' 06/22/2019'}
添加回答
举报
0/150
提交
取消