I am using a Python script to find and replace certain strings in text files of a given directory. I am using the fileinput
module to ease the find-and-replace operation, i.e., the file is read, text replaced and written back to the same file.
The code looks as follows:
import fileinput
def fixFile(fileName):
# Open file for in-place replace
for line in fileinput.FileInput(fileName, inplace=1):
line = line.replace("findStr", "replaceStr")
print line # Put back line into file
The problem is that the written files have:
- One blank line inserted after every line.
- Ctrl-M character at the end of every line.
How do I prevent these extra appendages from getting inserted into the files?
-
Try using
file.write()
instead ofprint
.You should not change the loop variable, and cannot write to the file you are reading from (text files are that way). Sorry, no in-place replace.
print
will write to the standard output (sys.stdout
) but can be redirected.print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as “print chevron.” In this form, the first expression after the >> must evaluate to a “file-like” object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.
Suggested replace function, not in-place replace (
outfile
is a file-like object):import fileinput def fixFile(fileName, outfile): # Open file for in-place replace for line in fileinput.FileInput(fileName, inplace=1): outline = line.replace("findStr", "replaceStr") outfile.write(outline) # Put line into outfile
-
Use
print line,
or
file.write(line)
to fix extra newlines.
As of Ctrl-M - that is probably caused by input files in DOS encoding.
Ashwin : Thanks. Your solution removes the blank lines but still has the Ctrl-M characters. Another person has already suggested an answer which solves both for me. -
your newlines are coming from the print function
use:
import sys
sys.stdout.write ('some stuff')
and your line breaks will go away
Ashwin : Thanks a lot! This fixed both extra lines & Ctrl-M characters. I have chosen your reply as the solution to this problem. Go ahead & add some extra information to your answer if you wish. -
Use instead of
print line # Put back line into file
this:
print line, # Put back line into file
-
Hey gimel.
Don't you need to define outfile somewhere before you can write to it? I'm not asserting this. I am asking.
-
Change the first line in your for loop to:
line = line.rstrip().replace("findStr", "replaceStr")
kmote : Please explain the down-vote. This method addresses the newline problem (although I was not able to reproduce the Ctrl-M problem, so cannot speak to it).
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.