Py学习  »  Python

迭代txt文件python

henning • 5 年前 • 1462 次点击  

我正试图迭代一个简单的.txt文件,其中包含一系列的食谱。 我想要的是我的循环找到所有以-成分开始的部分,添加每一行成分 到一个列表中,继续下一组原料,并添加这些原料,直到我有一个完整的列表,包括 文件中的所有成分。一般的文件结构如下:

  • 意面沙拉
  • 说明: BLA BLA

  • 成分:

  • 成分1
  • 成分2
  • 成分3

  • 准备: BLA BLA BLA

以下是迄今为止我的代码示例:

import os
import sys

def get_ingredients_from_file():
    recipe_file = open("/Filepath/", "r")
    final_ingredients_list = list()

    for line in recipe_file:                                                                                   
        if line.startswith("-Ingredients"):                             
            # append every line until next section, then move on to next
            # ingredient section
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38635
 
1462 次点击  
文章 [ 5 ]  |  最新文章 5 年前