1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Regular Expressions in Python

  1. Jul 17, 2011 #1
    1. The problem statement, all variables and given/known data

    This isn't really a homewrok/coursework question, but I didn't know where to put it. I'm trying to teach myself python over the summer and I seem to have got stuck on regular expressions. I want to read a .txt file with data for various kinds of swallows, laden/unlden, their airspeed velocities, and the date they were observed. Then, print only the ones which conform to the correct format. I managed to get the File I/O part to work, but the regex part doesn't.

    2. The attempt at a solution

    import re

    # Open text file contining data
    reader = open('C:\file.txt', 'r')
    # Read all lines of data
    data = reader.readlines()
    # Print all lines of file
    for line in data:
    print line

    # Close reader
    reader.close()

    print "\n\n\nShow Matches\n\n"

    # Regular expression to find matching patterns in file
    record = re.search((African|European) (Unladen|Laden) (\d{1,2}m/s) (\d{1-4}-\d{1,2}-\d{1,2})', data)
    if record:
    print match.search(1) + "\t" + match.search(2) + "\t" + match.search(3) + "\t" + match.search(4)

    # Require input to close
    x = raw_input("\n\nPress Enter")
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted



Similar Discussions: Regular Expressions in Python
  1. Python question (Replies: 2)

  2. Python syntax (Replies: 5)

  3. Python Beginner (Replies: 3)

  4. Python question (Replies: 1)

Loading...