我在文本文件中有下面提到的文本,每個(gè)鍵都在一個(gè)新行中。我需要轉(zhuǎn)換為如下所述的 JSON 數(shù)據(jù),請(qǐng)任何人幫助解決這個(gè)問(wèn)題。示例文本文件name: Samuels heardpatient id : 7898received date: 06/22/2019firstname : scott deny dob : 06/22/2019必需的 JSON 數(shù)據(jù){ "name": "Samuels heard", "patient id": "7898", "received date": "06/22/2019", "firstname": "scott deny", "dob": "06/22/2019"}
1 回答

蝴蝶刀刀
TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試這個(gè)
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'}
添加回答
舉報(bào)
0/150
提交
取消