Python搜索文本文件,在字符串后面打印字符

时间:2013-11-13 00:31:46

标签: python string

我希望能够在文档中搜索给定的字符串,并找到每个实例的上下文。例如,在文档中搜索“Figure”并在该字符串后面返回X个字符(从“Figure-1 Super awesome figure。下一句”中返回“-1 Super awesome figure”。)

我知道如何打印:A)该字符串的每个实例

mystring = "Figure"
with open('./mytext.txt', 'r') as searchfile:
    for line in searchfile:
        if mystring in line:
            print(mystring)

但这没有任何帮助;或B)包含该字符串的每一行

for line in open('./mytext.txt', "r"):
    if "Figure" in line:
        print(line) 

返回整行前后的所有文本,这对我来说很麻烦。

我可以在“mystring”处拆分一行并在拆分后返回X个字符吗?或者是否有更好的方法?

3 个答案:

答案 0 :(得分:3)

我会这样做:

WANTED = 20 #or however many characters you want after 'Figure'

with open('mytext.txt') as searchfile:
    for line in searchfile:
        left,sep,right = line.partition('Figure')
        if sep: # True iff 'Figure' in line
            print(right[:WANTED])

请参阅:str.partition

答案 1 :(得分:0)

你可以这样做:

line = "Figure-1 Super awesome figure. next sentence."

search_line = line.split("Figure")

print search_line

# prints ['', '-1 Super awesome figure. next sentence.']

count = 0
for elem in search_line: 
    count += len(elem)

print count # how many chars after "Figure"

答案 2 :(得分:0)

import re
X = len("-1 Super awesome figure")
regex = re.compile("Figure.{%d}" % X)
for line in open("mytext.txt"):
  for m in regex.findall(line):
    print m

您可能需要通过“返回该字符串后面的X字符”来澄清您的意思。