简体   繁体   English

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

[英]Returning contents of a text file and printing different value

I am making a Recipe book, at the moment I have the ability to create a recipe, but now I am starting to build the module of searching and displaying stored recipes. 我正在制作一本食谱书,目前可以创建食谱,但是现在我开始构建搜索和显示已存储食谱的模块。

At the moment I have a .txt document with the contents along the lines of: 目前,我有一个.txt文档,其内容大致如下:

Williams Special Recipe 威廉姆斯特别食谱

Ingredients: 配料:

bread: 120 grams butter: 1234 grams 面包:120克黄油:1234克

Recipe Serves: 12 食谱服务:12

I then ask the user how many they are serving and based on how many the recipe serves, I need to multiply all the ingredients quantity by that number. 然后,我问用户要提供多少食物,并根据多少种食谱,我需要将所有配料数量乘以该数量。 I then need to print that off with the full recipe again. 然后,我需要重新打印完整的配方。

I was wondering how I would go about achieving this result, not asking specifically for a coded response as an answer, but I would greatly appreciate how I would approach this task and any specific functions required. 我想知道我将如何实现这一结果,而不是专门要求编码响应作为答案,但是我将非常感谢我将如何完成这项任务以及所需的任何特定功能。

I have also included my code so far, I appreciate the fact it is incredibly un-organised at the moment, and probably hard to understand, but I included it for any reference. 到目前为止,我还包含了我的代码,我欣赏它目前还非常杂乱无章,并且可能很难理解,但我将其包括在内以供参考。

(I have also created a .txt file of all the created recipes which will be implemented later on as a way of displaying to the user all recipes, but at the moment it is just set up for searching.) (我还创建了所有已创建食谱的.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)

I'm not the specialist in processing strings, but I'd approach your problem following way: 我不是处理字符串的专家,但是我会通过以下方式解决您的问题:
Save each ingredient on a new line. 将每种成分保存在新行中。
Split the loaded string by "\\n". 用“ \\ n”分隔加载的字符串。
Then process the list with some for-loops while creating two dicts, one for the actual data 然后使用一些for循环处理列表,同时创建两个字典,一个用于实际数据

 e.g. {"bread": 4, "butter": 7}  

and one for the types of each ingredient: 一种用于每种成分的类型:

 e.g. {"bread": grams, "butter": grams}  

The you should also save how many serves the recipe is written for, and maybe the order of the ingredients (dicts get stored in a random order): 您还应该保存为配方写的份量,以及配料的顺序(字典以随机顺序存储):

e.g. ["bread", "butter"]

After that, you can ask your costumer how many serves he has and then finally calculate and print the final results. 之后,您可以询问您的客户他有多少份服务,然后最终计算并打印最终结果。

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

...hopyfully you still have enough challange, and hopefully I understood your question correctly. ...希望您仍然有足够的挑战,希望我能正确理解您的问题。

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

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