При чтении текстового файла некоторые строки не обнаруживаются?

Если функция печатает на System.out, вы можете записать этот вывод с помощью метода System.setOut, чтобы изменить System.out, чтобы перейти к PrintStream, предоставленному вами. Если вы создаете PrintStream, подключенный к ByteArrayOutputStream, вы можете записать вывод как String.

Пример:

    // Create a stream to hold the output
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    // IMPORTANT: Save the old System.out!
    PrintStream old = System.out;
    // Tell Java to use your special stream
    System.setOut(ps);
    // Print some output: goes to your special stream
    System.out.println("Foofoofoo!");
    // Put things back
    System.out.flush();
    System.setOut(old);
    // Show what happened
    System.out.println("Here: " + baos.toString());

Эта программа печатает только одну строка:

    Here: Foofoofoo!
-1
задан pythoncoder 20 March 2019 в 05:28
поделиться

1 ответ

Вот более простая реализация того, чего вы пытаетесь достичь. Вы можете прочитать файл в список 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
0
ответ дан prithajnath 20 March 2019 в 05:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: