以下是文件中的信息:
"Part no.","Description","Price"
"453","Sperving_Bearing","9900"
"1342","Panametric_Fan","23400"
"9480","Converter_Exchange","93859"
我试图弄清楚如何打开文件,然后使用零件号将其内容存储到字典中。作为键,其他信息作为值。所以我希望它看起来像这样:
{Part no.: "Description,Price", 453: "Sperving_Bearing,9900", 1342: "Panametric_Fan,23400",9480: "Converter_Exchange,93859"}
我能够将文件中的文本存储到一个列表中,但我不确定如何为一个键分配多个值。我试图在不导入任何模块的情况下做到这一点。我一直在使用基本的 str 方法、list 方法和 dict 方法。到目前为止,这是我的代码:(我假设将正确输入文件名)
textname = input("ENter a file")
thetextfile = open(textname,'r')
print("The file has been successfully opened!")
thetextfile = thetextfile.read()
file_s = thetextfile.split()
holder = []
ck = 0
for c in range(len(file_s)):
holder.append(file_s[c])
ck = ck+1
if(ck == 3):
holder.insert(c,'\n')
count = 0
holder_string = "".join(holder)
holder = holder_string.split("\n")
wordlist = {}
#kind of stuck here.