簡體   English   中英

在讀取文本文件時,未檢測到某些行?

[英]While reading text file some lines are not detected?

text_file.txt

我得到的是第一個打印語句的輸出,而不是第二個打印語句的輸出。請向我提供正確的代碼,我需要編碼或解碼嗎? 請幫我即時了解python3

這是您要實現的目標的更直接的實現。 您可以將文件讀入Python列表,並通過Python列表索引引用每一行

with open('text_file.txt','r') as f: # automatically closes the file
    input_file = f.readlines() # Read all lines into a Python list

for line_num in range(len(input_file)):
    if "INBOIS BERCUKAI" in input_file[line_num]:
        print(input_file[line_num + 2]) # offset by any number you want
    # same for other if statements

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM