我目前正在写关于首席执行官自恋的硕士论文。为了衡量它,我必须进行财报电话文本分析。我按照 python 中提供的答案编写了一段代码这个链接 https://stackoverflow.com/questions/36559356/extract-values-between-two-strings-in-a-text-file%20answer,这使我能够从财报电话会议记录中提取问题和解答部分。该文件如下所示(称为“testoestratto.txt”):
..............................
Delimiter [1]
..............................
A text that I don't need
..............................
Delimiter CEO [2]
..............................
I need this text
..............................
Delimiter [3]
..............................
[...]
..............................
Delimiter CEO [n-1]
..............................
I also need this text
..............................
Delimiter [n]
..............................
我还有另一个 txt 文件('lista.txt'),我在其中从记录中提取了所有分隔符:
Delimiter [1]
Delimiter CEO [2]
Delimiter [3]
[...]
Delimiter CEO [n-1]
Delimiter [n]
我想做的是从 Delimiter CEO [2] 和 Delimiter [3] 之间以及 Delimiter CEO [n-1] 和 Delimiter [n] 之间的“testoestratto.txt”中提取文本。提取的文本必须写入“test.txt”中。因此,如果“lista.txt”中的分隔符包含单词“CEO”,我需要“testoestratto.txt”中位于该特定分隔符和“lista.txt”中不包含单词“的下一个分隔符”之间的文本首席执行官”在其中。为此,我编写了以下代码:
with open('testoestratto.txt','r', encoding='UTF-8') as infile, open('test.txt','a', encoding='UTF-8') as outfile, open('lista.txt', 'r', encoding='UTF-8') as mylist:
text= mylist.readlines()
text= [frase.strip('\n') for frase in text]
bucket=[]
copy = False
for i in range(len(text)):
for line in infile:
if line.strip()==text[i] and text[i].count('CEO')!=0 and text[i].count('CEO')!= -1:
copy=True
elif line.strip()== text[i+1] and text[i+1].count('CEO')==0 or text[i+1].count('CEO')==-1:
for strings in bucket:
outfile.write(strings + '\n')
elif copy:
bucket.append(line.strip())
但是,“test.txt”文件是空的。你可以帮帮我吗?
附: :我是Python的初学者,所以如果代码很乱,我想道歉