[英]how to read txt file data and convert into nested dictionary in python
[英]read .txt file into a manipulatable string to convert into dictionary
我有一個包含以下內容的 .txt 文件:
ACC210:
路德,馬丁
司布真,查爾斯
CS121P:
班揚,約翰
亨利,馬修
路德,馬丁
CS132S:
卡爾文,約翰
諾克斯,約翰
歐文,約翰
我需要創建一個字典,所以它看起來像這樣
{'ACC210':['Luther,Martin','Spurgeon,Charles']'CS121P':['Bunyan,John''Henry,Matthew''Luther,Martin']'CS132S':['Calvin,John''諾克斯,約翰''歐文,約翰']}
我該怎么做?
這是創建字典的方法...
# Creating an empty dictionary
myDict = {}
# Adding list as value
myDict["key1"] = [1, 2]
myDict["key2"] = ["Geeks", "For", "Geeks"]
所以,對於你的情況,
MyDict={}
MyDict["ACC210:"]=["Luther, Martin", "Spurgeon, Charles"]
MyDict["CS121P:"]= ... (continue)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.