Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

counting into a new line

counting into a new line

counting into a new line

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.


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(str(x).replace(",", "\n"))

print 'Total locations is approximately: ', z

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

Again, any help is appreciated.

RE: counting into a new line

Compile your patterm to a re and use the multiline flag when you compile.

"... 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 Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close