在行块中循环时出现意外行为

2024-09-30 02:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件,其中包含11行每项块。我想遍历每个块并提取块中每行的数据。我是这样做的:

file_removed = open("input_removed.txt")
json_result = open("output_json.json", "w+")
datalist = []
while True:

    data = {}

    name = next(file_removed)
    name = re.sub("\n", "", name)

    data["name"] = name
    familyName = next(file_removed)
    familyName = re.sub("\n", "", familyName)

    data["familyName"] = familyName

    wGuideline = next(file_removed)
    wGuideline = re.sub("Watering guidelines\s+","", wGuideline)
    wGuideline = re.sub("\n", "", wGuideline)
    data["water"] = wGuideline

    FerLine = next(file_removed)
    FerLine = re.sub("Fertilizer suggestions\s+ ","",FerLine)
    FerLine = re.sub("\n", "", FerLine)
    data["fertilizer"] = FerLine

    MistLine = next(file_removed)
    MistLine = re.sub("Mist requirements\s+","",MistLine)
    MistLine = re.sub("\n", "", MistLine)
    data["mist"] = MistLine

    LightLine = next(file_removed)
    LightLine = re.sub("Light preferences\s+","", LightLine)
    LightLine = re.sub("\n", "", LightLine)
    data["light"] = LightLine

    TempLine = next(file_removed)
    TempLine = re.sub("Temperature preference\s+","",TempLine)
    TempLine = re.sub("\n", "", TempLine)
    data["temperature"] = TempLine

    print(TempLine)
    phLine = next(file_removed)
    phLine = re.sub("pH range\s+", "", phLine)
    phLine = re.sub("\n", "", phLine)
    data["ph"] = phLine

    AcidLine = next(file_removed)
    AcidLine = re.sub("Acidity preference\s+", "",TempLine)
    AcidLine = re.sub("\n", "", TempLine)
    data["acid"] = AcidLine

    ToxicLine = next(file_removed)
    ToxicLine = re.sub("Toxicity\s+", "",AcidLine)
    ToxicLine = re.sub("\n", "", AcidLine)
    data["toxic"] = ToxicLine

    ClimateLine = next(file_removed)
    ClimateLine = re.sub("Climate\s+", "",ClimateLine)
    ClimateLine = re.sub("\n", "", ClimateLine)
    data["climate"]= ClimateLine

    datalist.append(data)
    try:
        next(file_removed)
    except StopIteration:
        break;

您可以看到我实现的print(TempLine)来检查我的版本是否正常工作。但是在第一次迭代之后,每个WHILE循环只迭代一行

有人能为我解释一下这种行为吗


Tags: nameredatafilenextremovedfamilynametempline
1条回答
网友
1楼 · 发布于 2024-09-30 02:18:49

问题是try块中的最后一个next()读取下一个块的第一行,但没有捕获它,因此该行丢失。每个迭代读取12条记录,而不是11条,但您只处理11条

试试这个(有两个新行和一个换行):

import re

file_removed = open("input_removed.txt")
json_result = open("output_json.json", "w+")
datalist = []
name = None                                   # Added

while True:

    data = {}

    if name is None:                          # Added
        name = next(file_removed)

    name = re.sub("\n", "", name)

    data["name"] = name
    familyName = next(file_removed)
    familyName = re.sub("\n", "", familyName)

    data["familyName"] = familyName


    wGuideline = next(file_removed)
    wGuideline = re.sub("Watering guidelines\s+","", wGuideline)
    wGuideline = re.sub("\n", "", wGuideline)
    data["water"] = wGuideline

    FerLine = next(file_removed)
    FerLine = re.sub("Fertilizer suggestions\s+ ","",FerLine)
    FerLine = re.sub("\n", "", FerLine)
    data["fertilizer"] = FerLine

    MistLine = next(file_removed)
    MistLine = re.sub("Mist requirements\s+","",MistLine)
    MistLine = re.sub("\n", "", MistLine)
    data["mist"] = MistLine

    LightLine = next(file_removed)
    LightLine = re.sub("Light preferences\s+","", LightLine)
    LightLine = re.sub("\n", "", LightLine)
    data["light"] = LightLine

    TempLine = next(file_removed)
    TempLine = re.sub("Temperature preference\s+","",TempLine)
    TempLine = re.sub("\n", "", TempLine)
    data["temperature"] = TempLine

    print(TempLine)
    phLine = next(file_removed)
    phLine = re.sub("pH range\s+", "", phLine)
    phLine = re.sub("\n", "", phLine)
    data["ph"] = phLine

    AcidLine = next(file_removed)
    AcidLine = re.sub("Acidity preference\s+", "",TempLine)
    AcidLine = re.sub("\n", "", TempLine)
    data["acid"] = AcidLine

    ToxicLine = next(file_removed)
    ToxicLine = re.sub("Toxicity\s+", "",AcidLine)
    ToxicLine = re.sub("\n", "", AcidLine)
    data["toxic"] = ToxicLine

    ClimateLine = next(file_removed)
    ClimateLine = re.sub("Climate\s+", "",ClimateLine)
    ClimateLine = re.sub("\n", "", ClimateLine)
    data["climate"]= ClimateLine

    datalist.append(data)
    try:
        name = next(file_removed)          # Changed
    except StopIteration:
        break;

对这段代码还有其他的改进,但是进一步的修改会影响到眼前的问题

相关问题 更多 >

    热门问题