繁体   English   中英

python 3.x需要帮助,将1个文本文件拆分为4个词典

[英]python 3.x need help spliting 1 text file into 4 dictionaries

有人可以帮助我解决我遇到的问题吗? 我正在尝试编写一些代码,使用户可以选择称为日记的特定文件,然后将其与当前的技能水平进行比较,每个文件都有一个简单,中等,困难和精锐的部分,我希望将它们放在单独的词典中,我可以得到它来打印整个文件中的正确信息,但我不知道如何将其拆分为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
,
}

我猜想,您所苦恼的唯一部分就是如何使用给定的文件结构填写四个词典。

如果您确定这些文件不会被您以外的任何人/其他人篡改,并且可以使用不安全和肮脏的代码,则可以执行以下操作:

exec(file_contents)

这是因为文件结构的内容已经是有效的python,因此它将在被调用的范围内执行它。 因此,执行它之后,您可以在调用它的作用域中访问变量easy_levels, medium_levels, hard_levels, elite_levels 请注意,这是假设您在尝试访问的任何日记中正确定义了那些变量,如果每个日记中定义的变量可能会更改,则应使用更安全的方法(或访问locals()的丑陋方法)。

尝试这个:

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}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM