目的是根据文件的单词制作字典。所以我为open和read文件定义了以下函数,然后用文件的单词创建一个列表。我测试了这个函数并且它正常工作,但当我调用它将列表转移到字典时,没有实现完整的字典。
fild = dict()
def file2word():
fname = input("Enter a file name: ")
try:
fhand = open(fname)
except:
print("File cannot open",fname)
exit()
count = 0
val = list()
for line in fhand:
li = line.split()
val = val + li
count = count + 1
return(val)
i = 0
lst = file2word()
while True:
x = lst.pop(i)
fild[x] = "done"
if i == len(lst):break
print(fild)