【发布时间】:2021-01-19 10:06:34
【问题描述】:
我是 python 新手,请原谅我的愚蠢。我正在尝试遍历文件(.txt)中的字典。但是,我只想遍历字典中的键,而不是值。我已经尝试了几个小时,但没有取得任何进展。感谢您的帮助。
kluizen = {
11;kaasstengel,
1;geheim,
5;kluisvanpietje,
12;z@terd@g
} #the number of a locker with the password afterwards.
f = open('fa_kluizen.txt', 'r')
contents = f.read()
print(contents)
for numbers in contents: print(numbers)
f.close()
【问题讨论】:
-
for key in dictionary:如果你遍历字典,你会得到键。 -
分享你的尝试(代码)和你的文件样本
-
在添加更多信息时编辑您的帖子,而不是在评论中并解释什么是什么
-
您应该在字典中使用冒号而不是分号。您要导入的文本文件是否有分号?
标签: python dictionary for-loop key text-files