这是我拥有的文本文件的一部分
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.
我正在尝试迭代以获取参与者和采访者的翻译。这是我的代码。
while True:
interviewer = f.readline()
interviewer_translation = f.readline()
participant = f.readline()
participant_translation = f.readline()
...
if not participant_translation: break
但是,上面的代码试图逐行获取它,但是由于翻译有时要花费几行或更多行,因此无法正常工作。有没有一种方法可以不必使用readline?
如果参与者行和采访者行始终仅占一行,并且看起来始终相同,那么您可以使用类似的方法:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句