任何人都可以帮我解决我遇到的问题吗?我试图让一些代码让用户选择名为diarys的特定文件,然后将其与当前技能水平进行比较,每个文件都有一个简单,中等,硬,精英部分,我想把它放在单独的字典中,我可以得到它从整个文件打印正确的信息,但我不知道如何将其分成4个单独的词典
# shows all osrs diarys
def diary_selection():
diary_options = {
0 : 'ardougne', 1 : 'desert', 2 : 'falador', 3 : 'fremennik', 4 : 'kandarin',
5 : 'lumbridge', 6 : 'morytania', 7 : 'varrock', 8 : 'western', 9 : 'wilderness'
}
print(diary_options)
# if not a correct number gives prompt to retry
while True:
try:
d_sel = int(input ("\nType in the number next to the diary: "))
diary_select = d_sel
if not (0 < diary_select > 9):
print('option is valid')
break
else:
print(" invalid option.")
diary_selection()
except ValueError:
print(" invalid option, numbers only, please try again.")
# gets the name of the diary acording to what number was pressed
current_diary = diary_options.get(diary_select)
#creats a filename for spicific diary
diary_file_name = str((current_diary + "_diary.txt"))
print (diary_file_name,"\n")
#searches for file name in folder ./diary_requirements
f = open("./diary_requirements/"+diary_file_name,"r")
file_contents = f.read()
return file_contents
我试图操作的文件是在这种格式的txt文件中构建的,具有简单,中等,硬,精英级别。
easy_levels = {
"Attack" : 0
, "Defense" : 0
, "Strength" : 0
, "Hitpoints": 0
, "Range" : 30
, "Prayer" : 0
, "Magic" : 0
, "Cooking" : 0
, "Woodcutting" : 0
, "Fletching" : 20
, "Fishing" : 0
, "Firemaking" : 0
, "Crafting" : 0
, "Smithing" : 0
, "Mining" : 15
, "Herblore" : 0
, "Agility" : 0
, "Thieving" : 0
, "Slayer" : 0
, "Farming" : 0
, "Runecrafting" : 0
, "Hunting": 9
, "Construction" : 0
,
}
medium_levels = {
"Attack" : 0
, "Defense" : 0
, "Strength" : 0
, "Hitpoints": 0
, "Range" : 30
, "Prayer" : 0
, "Magic" : 0
, "Cooking" : 42
, "Woodcutting" : 35
, "Fletching" : 5
, "Fishing" : 46
, "Firemaking" : 35
, "Crafting" : 0
, "Smithing" : 0
, "Mining" : 40
, "Herblore" : 0
, "Agility" : 37
, "Thieving" : 0
, "Slayer" : 0
, "Farming" : 0
, "Runecrafting" : 0
, "Hunting": 31
, "Construction" : 0
,
}
答案 0 :(得分:0)
我猜你唯一要努力的部分就是如何在给出具有你描述的结构的文件的情况下填写四个词典。
如果您确定这些文件不会被除您之外的任何人/任何人更改,并且您可以使用不安全的&amp;脏代码,你可以这样做:
exec(file_contents)
这样做,因为你的文件结构的内容已经是有效的python,它在它被调用的范围内执行它。因此,在执行它之后,您可以访问您调用它的范围中的变量easy_levels, medium_levels, hard_levels, elite_levels
。 请注意这假设您在尝试访问的任何日记中正确定义了这些变量,如果每个日记定义的变量可能发生变化,您应该使用更安全的方法(或者丑陋的快速黑客访问本地人( ))。
答案 1 :(得分:0)
试试这个:
import os
import imp
from pprint import pprint
# shows all osrs diarys
def diary_selection():
diary_options = {
0 : 'ardougne', 1 : 'desert', 2 : 'falador', 3 : 'fremennik', 4 : 'kandarin',
5 : 'lumbridge', 6 : 'morytania', 7 : 'varrock', 8 : 'western', 9 : 'wilderness'
}
print(diary_options)
# if not a correct number gives prompt to retry
while True:
try:
d_sel = int(input ("\nType in the number next to the diary: "))
diary_select = d_sel
if diary_select in diary_options:
print('option is valid')
break
else:
print(" invalid option.")
#diary_selection()
except ValueError:
print(" invalid option, numbers only, please try again.")
# gets the name of the diary acording to what number was pressed
current_diary = diary_options.get(diary_select)
#creats a filename for spicific diary
diary_file_name = str((current_diary + "_diary.txt"))
print (diary_file_name,"\n")
#searches for file name in folder ./diary_requirements
#f = open("./diary_requirements/"+diary_file_name,"r")
#file_contents = f.read()
#return file_contents
foo = imp.load_source('userInfo', os.getcwd() + '/diary_requirements/' + diary_file_name)
print('{}\nEasy levels\n{}'.format('-'*40, '-'*40))
pprint(foo.easy_levels)
print('{}\nMediyum levels\n{}'.format('-'*40, '-'*40))
pprint(foo.medium_levels)
diary_selection()
输出(python):
{0: 'ardougne', 1: 'desert', 2: 'falador', 3: 'fremennik', 4: 'kandarin', 5: 'lumbridge', 6: 'morytania', 7: 'varrock', 8: 'western', 9: 'wilderness'}
Type in the number next to the diary: 6
option is valid
morytania_diary.txt
----------------------------------------
Easy levels
----------------------------------------
{'Agility': 0,
'Attack': 0,
'Construction': 0,
'Cooking': 0,
'Crafting': 0,
'Defense': 0,
'Farming': 0,
'Firemaking': 0,
'Fishing': 0,
'Fletching': 20,
'Herblore': 0,
'Hitpoints': 0,
'Hunting': 9,
'Magic': 0,
'Mining': 15,
'Prayer': 0,
'Range': 30,
'Runecrafting': 0,
'Slayer': 0,
'Smithing': 0,
'Strength': 0,
'Thieving': 0,
'Woodcutting': 0}
----------------------------------------
Mediyum levels
----------------------------------------
{'Agility': 37,
'Attack': 0,
'Construction': 0,
'Cooking': 42,
'Crafting': 0,
'Defense': 0,
'Farming': 0,
'Firemaking': 35,
'Fishing': 46,
'Fletching': 5,
'Herblore': 0,
'Hitpoints': 0,
'Hunting': 31,
'Magic': 0,
'Mining': 40,
'Prayer': 0,
'Range': 30,
'Runecrafting': 0,
'Slayer': 0,
'Smithing': 0,
'Strength': 0,
'Thieving': 0,
'Woodcutting': 35}