简体   繁体   English

从文本文件读取,而不必使用readline()

[英]Reading from textfile without having to use readline()

This is a portion of a textfile I have 这是我拥有的文本文件的一部分

Participant: Interviewer 
Translation: <english>Mhlongo.</english> Okay Monde, what languages do you typically use with your family and why? 
        :  
Participant: Participant 
Translation: Okay <english>it was Zulu, eh and Sotho, eh:</english> my mom is Sotho and my father is Zulu so we her language most of the time. 
        :  
Participant: Interviewer 
Translation: Mh, and so <english>you speak</english> <english>you speak</english>. What languages or language do you use with friends and why? 
        :  
Participant: Participant 
Translation:  Eh, isiZulu. 

I am trying to iterate through to get participant and interviewer translations. 我正在尝试迭代以获取参与者和采访者的翻译。 This is the code I have for it. 这是我的代码。

while True:
    interviewer = f.readline()
    interviewer_translation = f.readline()
    participant = f.readline()
    participant_translation = f.readline()
    ...
    if not participant_translation: break 

However, the above code tries to get it line by line but that doesn't work since the translation sometimes takes a couple of lines or more. 但是,上面的代码试图逐行获取它,但是由于翻译有时需要几行或更多行,因此无法正常工作。 Is there a way I can do it without having to use readline? 有没有一种方法可以不必使用readline?

You can read line by line using f.readline() and concatenate up to a record delimiter, then process the concatenated chunk, eg: 您可以使用f.readline()读取并连接到记录定界符,然后处理连接的块,例如:

def process(participant, translation):
    pass

participant = None
translation = ''
for line in f:
    if line.startswith('Participant: '):
        if participant:
            process(participant, translation)
        participant = line
        translation = ''
    elif participant and line.startswith('Translation: '):
        translation += line
process(participant, translation)

Or you can use f.read(size) function to read a bigger chunk of the file or whole file, if size argument is ommited: 或者,如果忽略size参数,则可以使用f.read(size)函数读取文件或整个文件的更大块:

>>> f.read()
'This is the entire file.\n'

Then you can use multilne regex to get you meaningful chunks of text from it, eg entire records: 然后,您可以使用multilne正则表达式从中获取有意义的文本块,例如整个记录:

>>> re.findall('(?P<record>^Participant:.*?)(?=(?:Participant:|\Z))', text, re.S | re.M)
['Participant: Interviewer\nTranslation: <english>Mhlongo.</english> Okay Monde, what languages do you typically use with your family and why?\n        :\n', 'Participant: Participant\nTranslation: Okay <english>it was Zulu, eh and Sotho, eh:</english> my mom is Sotho and my father is Zulu so we her language most of the time.\n        :\n', 'Participant: Interviewer\nTranslation: Mh, and so <english>you speak</english> <english>you speak</english>. What languages or language do you use with friends and why?\n        :\n', 'Participant: Participant\nTranslation:  Eh, isiZulu.\n']

Whatever feels more comfortable for you. 任何让您感到舒适的东西。 Be careful with reading large files at once though as they may not fit into available memory. 但是,一次读取大文件时要小心,因为它们可能不适合可用内存。

If the participant- and the interviewer-line always only take one line and always look the same, then you could use something like that: 如果参与者行和采访者行始终只占一行,并且看起来总是相同的,那么您可以使用类似的方法:

p_translation = ""
i_translation = ""
interviewer = False
for line in f:
    if line.startsWith("Participant: Participant"):
        #This would be the place to process i_translation
        #because now the translation of the interviewer was
        #fully read
        interviewer = False
        p_translation = ""
    elif line.startsWith("Participant: Interviewer"):
        #This would be the place to process p_translation
        #because now the translation of the participant was
        #fully read
        interviewer = True
        i_translation = ""
    else:
        if interviewer:
            i_translation += line
        else:
            p_translation += line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM