parse text file

Pradeep Jindal praddyjindal at gmail.com
Tue Oct 30 03:28:08 EDT 2007


On Tuesday 30 Oct 2007 12:06:57 pm william paul wrote:
> Hi:
>
> I am new to this list and new to Python. I have a text file that looks
> like: <values> 4 50
> <values> 3 900
> <values] 7 400
> ...
> <values} 9 70
>
> I want to be able to remove from each line everything up to ">" or "]"
> sign. For example: 4 50
> 3 900
>  7 400
>
> ...
> 9 70
>
> How can I do this?
>
> Thank you
>
> William
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com

You can use regular expressions module like this. 

>>> import re
>>> match_upto_brackets = re.compile(r'.*[>\]}](.*)')
>>> match_upto_brackets.sub(r'\1', '<values> 4 50').strip()
'4 50'
>>> match_upto_brackets.sub(r'\1', '<values] 7 400').strip()
'7 400'
>>> match_upto_brackets.sub(r'\1', '<values} 9 70').strip()
'9 70'
>>>

Pradeep



More information about the Python-list mailing list