Python 返回文本文件的内容并打印不同的值

Python 返回文本文件的内容并打印不同的值,python,Python,我正在制作一本食谱书,目前我有能力创建一个食谱,但现在我开始构建搜索和显示存储食谱的模块 目前,我有一个.txt文档,内容大致如下: 威廉姆斯特殊配方 成分: 面包:120克 黄油:1234克 菜谱:12份 然后我问用户他们提供了多少,根据食谱提供了多少,我需要将所有配料的数量乘以这个数字。然后我需要再次打印出完整的配方 我想知道我将如何实现这一结果,而不是专门要求一个编码响应作为答案,但我非常感谢我将如何处理这项任务和所需的任何特定功能 到目前为止,我还包括了我的代码,我很感激目前它是难以置信

我正在制作一本食谱书,目前我有能力创建一个食谱,但现在我开始构建搜索和显示存储食谱的模块

目前,我有一个.txt文档,内容大致如下:

威廉姆斯特殊配方

成分:

面包:120克 黄油:1234克

菜谱:12份

然后我问用户他们提供了多少,根据食谱提供了多少,我需要将所有配料的数量乘以这个数字。然后我需要再次打印出完整的配方

我想知道我将如何实现这一结果,而不是专门要求一个编码响应作为答案,但我非常感谢我将如何处理这项任务和所需的任何特定功能

到目前为止,我还包括了我的代码,我很感激目前它是难以置信的无组织的,可能很难理解,但我包括它以供参考

我还创建了一个包含所有已创建配方的.txt文件,稍后将实现该文件,作为向用户显示所有配方的一种方式,但目前它只是为搜索而设置的

#Recipe Task

import os.path

def file_(n):
    if n == "listR" :
        list_f = open("list_recipes.txt", "a+")
        list_f.write(new_name + "\n")
    if n == "oar": #open append read
        f=open(new_name + ".txt","a+")
    elif n == "c": #closes file
        f.close()


def print_line(x): #ease of printing multiple lines to break up text
    for c in range(x):
        print ""

def new_ingredients(): #adding new ingredients
    f.write("Ingredients:" + "\n" + "\n")
    fin_ingredient = False
    while fin_ingredient != True :
        input_ingredient = raw_input("New ingredient:" + "\n").lower()
        split_ingred = input_ingredient.split()
        if input_ingredient == "stop": #stops asking questions when user types 'stop'
            fin_ingredient = True
        else :
            f.write(split_ingred[0] + ":" + "  " + split_ingred[1] + " " + split_ingred[2] + "\n")

def search_recipe(n): #searching for recipes
    n = n + ".txt"
    if os.path.isfile('/Users/wjpreston/Desktop/' + n) == True :
        print "Recipe Found..."
        found_recipe = open(n)
        print found_recipe.read()
        append_serving = raw_input("Would you like to change the number of people you are serving?" + "\n").lower()
        if append_serving == "yes" :
            appended_serving = input("How many would you like to serve?" + "\n")

            with open(n) as f:  #here is my issue - not sure where to go with this!!
                list_recipe = f.readlines()   

            found_recipe.close()
        else :
            print "fail"



    else:
        print "No existing recipes under that name have been found."







print "Welcome to your Recipe Book"
print_line(3)

recipe_phase = raw_input("Are you 'creating' a recipe or 'viewing' an existing one?" + "\n").lower()

if recipe_phase == "creating":

    new_name = raw_input("Name of Recipe: " + "\n")
    file_("listR")
    file_("oar")
    f.write("------------" + "\n" + new_name + "\n" + "\n")
    print "Ingrediants required in the format 'ingredient quantity unit' - type 'stop' to end process"
    new_ingredients()
    new_num = input("Number serving:    ")
    f.write("\n" + "Recipe Serves: " + str(new_num) + "\n" "\n" + "\n")
    file_("c")



elif recipe_phase == "viewing":
    search = raw_input("Search for recipe: ")
    search_recipe(search)

我不是处理字符串的专家,但我会按照以下方式处理您的问题: 将每种配料保存在新的一行。 将加载的字符串拆分为\n。 然后用一些for循环处理列表,同时创建两个dict,一个用于实际数据

 e.g. {"bread": 4, "butter": 7}  
每种成分的类型各有一种:

 e.g. {"bread": grams, "butter": grams}  
此外,您还应保存编写食谱的份数,并且配料顺序可能会以随机顺序存储:

e.g. ["bread", "butter"]
在那之后,你可以问你的顾客他有多少发球,然后最后计算并打印最终结果

   for ing in ing_order:
       print ing+":", ing_amount[ing]*requested_serves/default_seves, ing_types[ing]

…希望你还有足够的挑战,希望我能正确理解你的问题。

这个问题是。。。相当广泛-我不确定你到底在找什么。对不起,我是新来的。不过,我还是要感谢你的回应。我试图找到一种方法,允许将配方存储在一个单独的文本文件中,然后在搜索时调用,但返回的成分值不同,具体取决于服务的人数。所以120克就够12个人吃了。但我提供24份,因此我希望它打印240克这种成分。我想向用户返回一个不同的值。希望这能澄清问题。是的,由于某种原因,上面包含的配方格式是错误的,配料都在自己的线上。非常感谢您的回复,非常感谢您在这方面的努力。我理解你在做什么,我很可能会支持你的方法,谢谢。一个问题是,我了解如何将文本文件的每一行存储在列表中,但是,如何确定这是否是一个需要应用上述规则的成分字段?@WJPreston创建一个名为成分的变量。使用for循环处理列表。如果当前元素为配料:,则将配料设置为True。然后,也可以在for循环中添加一些if语句,例如if元素[:13]==Recipe-services:process\u-services,elif-components==True:process\u-component。当然还有更优雅的方式,也许你能找到,但至少你有一个想法。啊,我现在开始了解更多,非常感谢你的努力。我是python新手,非常感谢您的回复。