script files with python (instead of tcsh/bash)?

E

Esmail

Hello David,

R. David Murray said:
Here's a more Pythonic way to do that:

with open('somefile') as f:
for line in f:
if 'somestring' in line:
#do something

In other words, you don't have to read the lines into a list first if all
you are going to do is iterate through them.

Cool .. stored away for future reference.

In this case I believe I needed the contents in a list because the line
I was looking for was above one that I could easily identify. Ie, once
I had the index, I could go back/up one index to find the line I needed
to process.

(The 'with' clause closes
the file at block exit...which is overkill if this is all the program
is doing since the file will be closed at program termination anyway,
but is a good habit to get in to.)

thanks for reminding me about the 'with' clause. I agree with closing
files and in general being mindful about allocated resources.

Cheers,
Esmail
 
E

Esmail

Hello David,

R. David Murray said:
Here's a more Pythonic way to do that:

with open('somefile') as f:
for line in f:
if 'somestring' in line:
#do something

In other words, you don't have to read the lines into a list first if all
you are going to do is iterate through them.

Cool .. stored away for future reference.

In this case I believe I needed the contents in a list because the line
I was looking for was above one that I could easily identify. Ie, once
I had the index, I could go back/up one index to find the line I needed
to process.

(The 'with' clause closes
the file at block exit...which is overkill if this is all the program
is doing since the file will be closed at program termination anyway,
but is a good habit to get in to.)

thanks for reminding me about the 'with' clause. I agree with closing
files and in general being mindful about allocated resources.

Cheers,
Esmail
 
P

Peter Otten

Esmail said:
Hello David,



Cool .. stored away for future reference.

In this case I believe I needed the contents in a list because the line
I was looking for was above one that I could easily identify. Ie, once
I had the index, I could go back/up one index to find the line I needed
to process.

Here's one way to avoid the list:

last_line = None
for line in f:
if "somestring" in line and last_line is not None:
# do something with last_line
last_line = line

Peter
 
E

Esmail

Peter said:
Here's one way to avoid the list:

last_line = None
for line in f:
if "somestring" in line and last_line is not None:
# do something with last_line
last_line = line

Peter


yup .. this will come in handy in case the file is too
big and or really needn't be all in memory. cool. thanks!

Esmail
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top