Modifying a row in a textfile

O

Olli Virta

Hi!

I got a textfile made out of database records. Is there an easy way to
modify rows in that file in case you have to take away some items here
and there from each rows.

Tanks! OV
 
D

Diez B. Roggisch

Olli said:
Hi!

I got a textfile made out of database records. Is there an easy way to
modify rows in that file in case you have to take away some items here
and there from each rows.

for line in inf.readlines():
if matches_criteria(line):
line = modify_line(line)
outf.write(line)


Diez
 
J

Jorgen Grahn

for line in inf.readlines():
if matches_criteria(line):
line = modify_line(line)
outf.write(line)

In other words, no. You need to put the results in another file.

/Jorgen
 
D

Dave Angel

Jorgen said:
In other words, no. You need to put the results in another file.

/Jorgen
If it is important that this ends up in the same file, you can simplify
the code by using fileinput.input() with the "inplace" paramater. It
renames the input file, redirects stdout to a new file with the original
name, and deletes the renamed file, all at appropriate times. There are
a few caveats, so be careful. The main caveat is to make sure you close
the file.

DaveA
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top