각 항목의 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)을 볼 수있다. 그러나 FIRST 반복 후에 각 WHILE 루프는 한 줄만 반복합니다!
나를 위해이 문제를 설명 할 수 있습니까?
오류 ... 입력 파일의 예를 제공해 주시겠습니까? 당신이 거기에서 많은 일을하고있는 것처럼 보입니다. ... –
@cdarke 하나 이상의 입력을 시도 할 수 있습니까? 기밀 이유로이 곳에 입력 할 수 없습니다. –
제 잘못입니다. 각 반복마다'next()'에 12 번의 호출이 있으며,'try' 블록에서 마지막으로 읽어 들인 라인을 포착하지는 않습니다. – cdarke