我有一个文本文件:
会计原则。消极保证条款。限制附属分配的条款。Business......Accounting原则的行:在“国际财务报告准则”的定义中定义。行政代理人: SVB......In :任何会计原则(如下文所定义)发生的事件及其变化结果.
在本文件中,“会计原则”出现了三次,“国际财务报告准则”出现了一次。
我试着在每个“会计原则”和“国际财务报告准则”之后提取300个字符(或300个单词)。现在,我只能在“会计原则”第一次出现之后提取字符,并为“会计原则”和“国际财务报告准则”分别编写代码。那么,我的问题是,在每次出现“会计原则”之后,如何提取300个字符,以及如何编写一个代码,使我能够同时处理“会计原则”和“国际财务报告准则”,而不是使用两个单独的代码?
非常感谢!
我的代码如下:
import os
sourcepath=os.listdir('try/')
for filename in sourcepath:
inputfile='try/'+filename
with open(inputfile, 'r') as f:
text=f.read()
index=text.index('Accounting Principles')
right=text[index: index+3000]
print(right)
import os
sourcepath=os.listdir('try/')
for filename in sourcepath:
inputfile='try/'+filename
with open(inputfile, 'r') as f:
text=f.read()
index=text.index('IFRS')
right=text[index: index+3000]
print(right)发布于 2018-04-07 13:55:49
该程序查找“会计原则”或“国际财务报告准则”的每一个实例,并将匹配的字符串与其末尾的30个字符一起打印出来。
import re
with open('x.in') as fp:
text = fp.read()
for m in re.finditer("Accounting Principles|IFRS", text):
print(text[m.start():m.end()+30])发布于 2018-04-07 14:11:54
可以使用re.sub在"Accounting Principles"或"IFRS"的任何位置创建标记,然后在full_string上进行迭代。
marked_data = re.sub('Accounting\sPrinciples|IFRS', '*', open('filename.txt').read())
new_data = [marked_data[i:i+3000] for i in range(len(marked_data)-3000)]https://stackoverflow.com/questions/49707915
复制相似问题