4 Replies - 1300 Views - Last Post: 01 December 2010 - 01:48 PM Rate Topic: -----

#1 Guest_Abel09*


Reputation:

Regex Python

Posted 30 November 2010 - 09:47 PM

I'm pulling in a file and would like to perform acouple regexs' on the txt file. My syntax looks like this.

f = open('Test.txt','r')
for line in f:
    print(line,end=' ')

Which reads and displays my txt file but i can't seem to run any regexs on it. Please help.

This post has been edited by JackOfAllTrades: 01 December 2010 - 09:35 AM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

Replies To: Regex Python

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Regex Python

Posted 01 December 2010 - 09:34 AM

That's some really basic code. Please, in the future, post your code in code tags, like so:

:code:

Now, what have YOU tried in terms of running regexes?
Was This Post Helpful? 0
  • +
  • -

#3 gabelgee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 10

Re: Regex Python

Posted 01 December 2010 - 12:50 PM

View PostAbel09, on 30 November 2010 - 08:47 PM, said:

I'm pulling in a file and would like to perform acouple regexs' on the txt file. My syntax looks like this.

f = open('Test.txt','r')
for line in f:
    print(line,end=' ')

Which reads and displays my txt file but i can't seem to run any regexs on it. Please help.

Was This Post Helpful? 0
  • +
  • -

#4 gabelgee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 10

Re: Regex Python

Posted 01 December 2010 - 01:01 PM

View Postgabelgee, on 01 December 2010 - 11:50 AM, said:

View PostAbel09, on 30 November 2010 - 08:47 PM, said:

I'm pulling in a file and would like to perform acouple regexs' on the txt file. My syntax looks like this.

f = open('Test.txt','r')
for line in f:
    print(line,end=' ')

Which reads and displays my txt file but i can't seem to run any regexs on it. Please help.

Thanks for the comment. I have tried things like
[f{1,}]
Have I created an object out of this statement. [f=open('test.txt','r')] (textfile). To be honest I searched and found a generator I could import my text file to and run statements but have had no luck.
I have a book here "Programming in Python 3" but I find it difficult to apply the examples.What I want to do is serach through a large text file as sys agvr and run regex on it. I also tried to import re which is something I don't know if I have to do?????
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Regex Python

Posted 01 December 2010 - 01:48 PM

Quick example:
import re

# File contents
# Man:test
# Other Man:test
# Man:test
# Other Man:test

# Compile the string to find
# In this case, if the line begins with "Man"
to_find = re.compile('^Man')

# Open the file for reading
with open('c:\sketch.txt', 'r') as f:
    # Read line by line
    for line in f:
        # Try to match compiled regex
        if to_find.match(line):
            # Found it, so print the whole line
            print (line.strip())



Output:
C:\Python31>python re.py
Man:test
Man:test


See also this page
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1