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
grep
Or: show your work.
--
Neil Cerutti
ciao
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
> 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
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()