extract substring by regex from a text file

A

Alessio

Hi,

I'm facing the problem in the subject:
- I have a text file that I need to parse for producing a specifical
string (Json like) extracting some information (substring) in it;
- I created regural expressions capable to locate these substrings in
my txt file;

now I don't know how to continue. What is the best way to locate some
string in a file and output them (with print command or in another
file)?

Thx in advance
 
N

Neil Cerutti

Hi,

I'm facing the problem in the subject:
- I have a text file that I need to parse for producing a specifical
string (Json like) extracting some information (substring) in it;
- I created regural expressions capable to locate these substrings in
my txt file;

now I don't know how to continue. What is the best way to locate some
string in a file and output them (with print command or in another
file)?

grep

Or: show your work.
 
A

Alessio

Thank you, I forgot to say that I already solved.
I used readlines() to read my text file, then with a for cicle I
extract line by line the substrings I need by regular expressions
(re.findall())

ciao
 
S

Stefan Behnel

Alessio, 17.04.2010 10:19:
I used readlines() to read my text file, then with a for cicle I
extract line by line the substrings I need by regular expressions
(re.findall())

Note that it's usually more efficient to just run the for-loop over the
file object, rather than using readlines() first. The latter will read all
lines into a big list in memory before doing any further processing,
whereas the plain for-loop will read line by line and let the loop body act
on each line immediately.

Stefan
 
P

Peter Otten

Alessio said:
I used readlines() to read my text file, then with a for cicle I
extract line by line the substrings I need by regular expressions

Just in case you didn't know:

for line in instream:
...

looks better, uses less memory, and may be a tad faster than

for line in instream.readlines():
...

Peter
 
A

Alessio

Just in case you didn't know:

    for line in instream:
        ...

looks better, uses less memory, and may be a tad faster than

    for line in instream.readlines():
        ...

Peter

Thanks for your suggestions, they are welcome... I'm at the beginning
with python.
I just changed my script to parse the file without readlines()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top