Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use fileinput to modify contents of a file?

Status
Not open for further replies.

jlowder

Technical User
May 7, 2002
10
0
0
US
I have a text file that I need to loop through in search of various strings. When I find these strings, I want to overwrite the lines with new text.

I get the impression the fileinput module is good for this, but I can't find an example of how to do it.. I'm thinking it would start along the lines of :

import fileinput
import string

for line in fileinput.input('c:\test.txt'):
rc = string.find(line,"some text")
if rc > -1:
line = "replacement text"

Only I'm not sure how to write out the next lines to the file because I never really opened a file (did I?).

Any suggestions?

Thanks
 
This may not be the best solution, depending on the size of your file, but something I have done in the past is read the entire file into an array and process it a line at a time, writing back what I wanted to keep.
Something along the lines of:
Code:
fil = open("myfile.txt","r")
t_str = fil.read()
fil.close

fil = open("myfile.txt","w")

fil_lines = string.split(fil,"\n")
for line in fil_lines:
   if (string.find(line,"some text") > -1:
      fil.write("replacement line\n")
   else:
      fil.write(line)

fil.close()

I haven't used fileinput before, so this may not be the best solution.

Another possibility would be to read it all into a string and then use a regular expression to replace on a pattern that looked something like:
Code:
"\n[^\n]*some text[^\n]*\n"

-Tarwn

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
That's the method I'm currently exploring, I was just wondering if Fileinput worked for that reason or not.

One question though. Do you know how to increase the line iteration while inside the loop? For example, I have a line which is never the same and has no reliable search features. I do, however, know that this line always shows up three lines after a predictable line of text. Is there anyway to increase the line count by 3 after finding this line and then replacing the line I want to replace?

I added a counter for this purpose, but it was kind of cludgy. Something like line+++ or whatever would be nice. Any ideas?

Thanks for the help.

Jason
 
You could implement a pseudo-counter inside that loop:
Code:
skip_ctr = 42
for line in fil_lines:
   #if the skip ctr = 3, write this line directly without searching
   if skip_ctr == 3:
      file.write(line)
   else:
      if (string.find(line,"some text") > -1:
         fil.write("replacement line\n")
      else:
         fil.write(line)
      #check for the text that denotes a skip is coming up
      if (string.find(line,"text that occurs 3 lines before skipping") > -1:
         skip_ctr = 1

   #increment the skip ctr
   skip_ctr += 1

This way the ctr is set to 1 only when it finds the lines that denotes that you are three lines beforethe one to not check. When that lines comes up you just write it without checking it and cnotinue on.
If you wanted to skip it altogether you could do a reverse check and put the main search code in the if stmt:
Code:
skip_ctr = 42
for line in fil_lines:
  if skip_ctr != 3:
      if (string.find(line,"some text") > -1:
         fil.write("replacement line\n")
      else:
         fil.write(line)
      #check for the text that denotes a skip is coming up
      if (string.find(line,"text that occurs 3 lines before skipping") > -1:
         skip_ctr = 1

   #increment the skip ctr
   skip_ctr += 1

This one will cause you to skip that line completely and not write it back to the output file.

-Tarwn


[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Hi Jason,

I don't know if this helps, but here is what I do. by reading in as lines you can randomly jump to anywhere in inFile - forwards and backwards.

inFile = open('filename', 'r').readlines()
iCounter = 0
for line in inFile:
iCounter = iCounter + 1
if line=='': #your test for a line
print inFile[iCounter+3] #show line 3 below
 
Hi,
I'm trying to use the first code posted above by Tarwn,
but keep getting a 'string is not defined' error. Do I need to import a module or am I doing something very silly ??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top