Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在行块中循环时出现意外行为_Python_Loops - Fatal编程技术网

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

Python 在行块中循环时出现意外行为,python,loops,Python,Loops,我有一个文件,其中包含每项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["n

我有一个文件,其中包含每项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;
您可以看到我实现的用于检查我的版本是否正常工作的打印(模板)。但是在第一次迭代之后,每个WHILE循环只迭代一行


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

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

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


可以对此代码进行其他改进,但进一步的更改会减少直接的问题。

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

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


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

Err。。。你能提供一个输入文件的例子吗?看起来你在那里做了很多工作,你不需要…@cdarke你可以尝试使用多个输入吗?由于机密原因,我不能在这里发布我的输入。我的错误。请注意,在每次迭代中有12个对
next()
的调用,您没有捕获
try
块中最后一个读取的行。在11个块之间是否有一个空行?如果没有,那么循环中最后一行的
next()
就是问题所在。@cdarke my bad,我在开始时忘记了下一行。让我再试一次。。。你能提供一个输入文件的例子吗?看起来你在那里做了很多工作,你不需要…@cdarke你可以尝试使用多个输入吗?由于机密原因,我不能在这里发布我的输入。我的错误。请注意,在每次迭代中有12个对
next()
的调用,您没有捕获
try
块中最后一个读取的行。在11个块之间是否有一个空行?如果没有,那么循环中最后一行的
next()
就是问题所在。@cdarke my bad,我在开始时忘记了下一行。让我再试一次谢谢,这解释了很多!谢谢,这解释了很多!
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;