Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting into a new line

Status
Not open for further replies.

cyphrix

Programmer
Nov 16, 2006
27
0
0
US
Ok, so the code below uses the re module to find a word 'located'. After it finds the word, it counts up to 10 words after it and extracts all of the words as one string. The only problem I am encountering is that I can't get the script to recognize a new line break, go to the next line, and then continue counting up to 10. If anyone can help, I would greatly appreciate it b/c its stumping me.


Code:
import re
_inputFileName = raw_input("Input File Name: ")
_outputFileName = raw_input("Output File Name: ")
_startingLine = '(located)'

_fin = open(_inputFileName, 'r')
_data = _fin.read()
_counter = 0

for _document in _data.split(_startingLine):
     _counter += 1
 
     locpat = 'located((?: \w+){1,10})'

     x = re.findall(locpat, _document)
     z = len(x)

     for line in x:
          print line
          f = open(_outputFileName, "w")
          f.write("Total locations is approximately ")
          f.write(`z`)
          f.write('\n\n\n')
          f.write(str(x).replace(",", "\n"))
          f.close()

print 'Total locations is approximately: ', z

raw_input("Press Enter To Close This Window.")




Again, any help is appreciated.
 
Compile your patterm to a re and use the multiline flag when you compile.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top