2017-12-30 48 views
0

各行に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; 

私のバージョンが動作しているかどうかを確認するために実装したプリント(TempLine)を見ることができます。しかし、最初の反復の後、各WHILEループは1つの行だけを繰り返します!

この動作を私に説明することはできますか?

+0

エラー...入力ファイルの例を教えてください。それはあなたがそこに多くの仕事をしているように見える... –

+0

@cdarke複数の入力を試すことができますか?私は機密理由のためにここに私の入力を掲載することはできません。 –

+0

私の悪いです。それぞれの反復で 'next()'を呼び出す呼び出しが12回あり、 'try'ブロックの最後の行で読み込まれた行はキャプチャしていないことに注意してください。 – cdarke

答えて

2

問題がtryブロックで最後next()ブロックの最初の行を読み取りますが、その行が失われているので、それをキャプチャしていないということです。各繰り返しは、12件のレコードを読み取りない11、しかし、あなたは唯一のプロセス11

は、この(二つの新しい行と1つの変更された行があります)試してみてください。これに作ることができる他の改善があります

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; 

をコードを変更することはできますが、それ以上の変更を行うとすぐに問題が発生しません。

+0

ありがとう、これはたくさん説明しました! –

関連する問題