我需要在Python中打开的txt文件中的' * START'和'* END'之前省略所有信息(因此.txt只是正文) 我们得到了一个字符串参数并写了。它继续写出原始的.txt而不仅仅是正文
def copy_file_2(s:str):
“如果它的参数是'Gutenberg trim',它将只复制Project Gutenberg文件的正文,省略前面和后面的”housekeeping“材料。”
infile_name = input("Please enter the name of the file to copy: ")
infile = open(infile_name, 'r', errors = 'ignore')
outfile_name = input("Please enter the name of the new copy: ")
outfile = open(outfile_name, 'w')
if s == 'Gutenberg trim':
infile_data = infile.readlines()
for i in range(len(infile_data)):
t = '{:5d}: {}'.format(i+1,infile_data[i])
if "*** START" in t:
outfile.write(t)
else:
for line in infile:
outfile.write(line)
infile.close()
outfile.close()
答案 0 :(得分:2)
print re.search("START(.*)END",open("some_file").read(),re.DOTALL).groups()[0]
我很确定你应该可以正常工作......