我是python的初学者,正在将其用于硕士论文,所以我对此并不了解。我有一堆年度报告(以txt格式)文件,我想选择“ ITEM1”之间的所有文本。和“ ITEM2”。我正在使用re包装。我的问题是,有时在那10k中有一个名为“ ITEM1A”的部分。我希望代码能够识别并停止在“ ITEM1A”。并在输出中输入“ ITEM1”之间的文本。和“ ITEM1A”。在本文所附的代码中,我试图使其停止在“ ITEM1A。”处,但是没有,它继续下去,因为“ ITEM1A”。在文件中多次出现。我希望使它停止在它看到的第一个位置。代码如下:
import os
import re
#path to where 10k are
saved_path = "C:/Users/Adrian PC/Desktop/Thesis stuff/10k abbot/python/Multiple 10k/saved files/"
#path to where to save the txt with the selected text between ITEM 1 and ITEM 2
selected_path = "C:/Users/Adrian PC/Desktop/Thesis stuff/10k abbot/python/Multiple 10k/10k_select/"
#get a list of all the items in that specific folder and put it in a variable
list_txt = os.listdir(saved_path)
for text in list_txt:
file_path = saved_path+text
file = open(file_path,"r+", encoding="utf-8")
file_read = file.read()
# looking between ITEM 1 and ITEM 2
res = re.search(r'(ITEM[\s\S]*1\.[\w\W]*)(ITEM+[\s\S]*1A\.)', file_read)
item_text_section = res.group(1)
saved_file = open(selected_path + text, "w+", encoding="utf-8") # save the file with the complete names
saved_file.write(item_text_section) # write to the new text files with the selected text
saved_file.close() # close the file
print(text) #show the progress
file.close()
如果有人对如何解决这个问题有任何建议,那就太好了。谢谢!
最佳答案
尝试以下正则表达式:
ITEM1\.([\s\S]*?)ITEM1A\.
添加问号会使它变得非贪婪,因此它将在第一次出现时停止