我有一个文本文件,我想将它加载到 Python 中的字典中。
文本看起来像这样,制表符分隔:
表格剂量参考药物药物名称activeingred INJECTABLE; 注射液 20,000 UNITS/ML LIQUAEMIN SODIUM 肝素钠注射液;注射液 40,000 UNITS/ML 肝素钠肝素钠注射液;注射液 5,000 UNITS/ML LIQUAEMIN SODIUM 肝素钠
现在我的代码看起来像这样,但它不起作用(列表索引超出范围,并且没有推送到字典中)。我不知道我哪里错了,不是程序员。谢谢你的帮助。
import sys
def load_medications(filename):
meds_dict = {}
f = open(filename)
l = " "
# print f.read()
for line in f:
fields = l.split("\t")
ApplNo = fields[0]
ProductNo = fields[1]
Form = fields[2]
Dosage = fields[3]
ProductMktStatus = fields[4]
TECode = fields[5]
ReferenceDrug = fields[6]
DrugName = fields[7]
ActiveIngred = fields[8]
meds = {
"ApplNo": ApplNo,
"ProductNo": ProductNo,
"Form": Form,
"Dosage": Dosage,
"ProductMktStatus": ProductMktStatus,
"TECode": TECode,
"ReferenceDrug": ReferenceDrug,
"DrugName": DrugName,
"ActiveIngred": ActiveIngred
}
meds_dict[DrugName] = meds
f.close()
return meds_dict
def main():
x = load_medications("druglist.txt")
print x
if __name__ == "__main__":
main()