简体   繁体   中英

Python use title or keyword to print paragraph from text file

I have an text file, and I want to search 'some keyword',and print that whole paragraph.

The text file is from pdf, so it is like:

  1. REFERENCES

[1] The Google Search Engine: Commercial search engine founded by the originators of P. aaa:// sssssss/. [2] The Open Directory Pro j: Web directory for over 2.5 million URLs. http://z.org/. [3] 'More Evil Than Dr. Evil?' http://sea r com/sere po r t/99/11- google.html. [4] Krishna Bharat and Mon ika R. Improved algorithms for topic distillation in a hyperlinked

Now I can only print one line

f = open('Desktop\\2002\\1. t x t','r')
lines = f. read lines()
for lines in lines:
   if "[1]" in lines:
       print(lines)

ans:The Google Search Engine: Commercial search engine

I want a keyword like 'REFERENCES', and print: [1] The Google Search Engine: Commercial search engine founded by the originators of P.

[2] The Open Directory Pro je ct: Web directory for over 2.5 million URLs. . . .

After your search has found the keyword you could assume how the next parts will look.

So you could skip the next line and after that print each line until another empty line is coming up.

f = open('Desktop\\2002\\1. t x t','r')
lines = f. read lines()
currentParagraph = False
for line in lines:
   if "[1]" in line and not currentParagraph:
       print(line)
       currentParagraph = True
       continue
   if currentParagraph:
       print(line)
       if line == "":
           break

You have to check however wheter there are line break symbols in the "empty" lines ("\n").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM