Replace text in file with Python - python

I'm trying to replace some text in a file with a value. Everything works fine but when I look at the file after its completed there is a new (blank) line after each line in the file. Is there something I can do to prevent this from happening.
Here is the code as I have it:
import fileinput
for line in fileinput.FileInput("testfile.txt",inplace=1):
line = line.replace("newhost",host)
print line
Thank you,
Aaron

Each line is read from the file with its ending newline, and the print adds one of its own.
You can:
print line,
Which won't add a newline after the line.

The print line automatically adds a newline. You'd best do a sys.stdout.write(line) instead.

print adds a new-line character:
A '\n' character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.

Related

Write new line at the end of a file

I am working with a numpy array in python. I want to print the array and its properties to a txt output. I want the text output to end with a blank line. How can I do this?
I have tried:
# Create a text document of the output
with open("demo_numpy.txt","w") as text:
text.write('\n'.join(map(str, [a,shape,size,itemsize,ndim,dtype])) + '\n')
And also:
# Create a text document of the output
with open("demo_numpy.txt","w") as text:
text.write('\n'.join(map(str, [a,shape,size,itemsize,ndim,dtype])))
text.write('\n')
However, when I open the file in GitHub desktop, I still get the indication that the last line of the file is "dtype"
When you do "\n".join( ... ) you will get a string of the following form:
abc\ndef\nghi\nhjk
-- in other words, it won't end with \n.
If your code writes another \n then your string will be of the form
abc\ndef\nghi\nhjk\n
But that does not put a blank line at the end of your file because textfiles are supposed to have lines that end in \n. That is what the Posix standard says.
So you need another \n so that the last two lines of your file are
hjk\n
\n
Python will not choke if you ask it to read a textfile where the final trailing \n is missing. But it also won't treat a single trailing \n in a textfile as a blank line. It would not surprise me to learn that GitHub does likewise.
This was solved using the Python 3.x print function, which automatically inserts a new line at the end of each print statement.
Here is the code:
with open("demo_numpy.txt","w") as text:
print(a, file = text)
text.close()
Note- apparently it is more appropriate to use the print function rather than .write when dealing with string values as opposed to binary files.

Comparing 2 variables when one is retrieved from text file

I am trying to write code that compares variable b with value retrieved from text file using linecache.getline
The problem is it will never print our "ITS WORKING" because the values never match, even if they do :-(
THE TEXT FILE: In the text file there is only one character and its "a"
Here is the code:
import linecache
b="a"
a=linecache.getline("TextFile.txt",1)
if a==b:
print("ITS WORKING")
According to the documentation, linecache.getline will include the trailing newline character, that's why your match does not work.
You probably need to strip the extra spaces at the end of line that is read.
a=linecache.getline("TextFile.txt",1).strip()
Keerthana:~ kiran$ cat TextFile.txt
a
Keerthana:~ kiran$ py Desktop/test.py
a
ITS WORKING
Keerthana:~ kiran$
Hope it helps!

When piping a file in windows to a python script, my \r are deleting my characters

I have a file like this:
A\r
B\n
C\r\n.
(By \r I'm referring to CR, and \n is LF)
And this script:
import fileinput
for line in fileinput.input(mode='rU'):
print(line)
When I call python script.py myfile.txt I get the correct output:
A
B
C
But when I call it like this: type myfile.txt|python script.py, I get this:
B
C
You see? No more "A".
What is happening? I thought the mode='rU' would take care of every newline problem...
EDIT: In Python 3 there is no such problem! Only in Python 2. But that does not solve the problem.
Thanks
EDIT:
Just for the sake of completeness.
- It happens also in Linux.
Python 3 handles every newline type (\n, \r or \r\n) transparently to the
user. Doesn't matter which one your file got, you don't have to worry.
Python 2 needs the parameter mode='rU' passed to fileinput.input to allow it
to handle every newline transparently.
The thing is, in Python 2 this does not work correctly when piping content
to it.
Having tried to pipe a file like this:
CR: \r
LF: \n
CRLF: \r\n
Python 2 just treats these two lines as just one line and if you try to print
every line with this code:
for i,line in enumerate(fileinput.input(mode='rU')):
print("Line {}: {}".format(i,line), end='')
It outputs this:
Line 0: CR:
LF:
Line 1: CRLF:
This doesn't happen in Python 3. There, these are 2 different lines.
When passing this text as a file, it works ok though.
Piping data like this:
LF: \n
CR: \r
CRLF: \r\n
Gives me a similar result:
Line 0: LF:
Line 1: CR:
CRLF:
My conclusion is the following:
For some reason, when piping data, Python 2 looks for the first newline
symbol it encounters and then on, it just considers that specific character
as a newline. In this example Python 2 encounters \r as the first newline
character and all the others (\n or \r\n) are just common characters.

python file read, write line by line

I am studying python file I/O. I made a simple program(main.py).
My goal is read line by line and write line by line.
fstream = open("input2.txt", 'r');
line = fstream.readline()
while line:
print(line);
line = fstream.readline()
fstream.close()
below are my input2.txt file
start.
hello world.
hello python.
I am studying file I/O in python
end.
when I run python program
python main.py
Then, result is ...
start.
hello world.
hello python.
I am studying file I/O in python
end.
That is not the same as I expected.
So I modified the main.py
fstream = open("input2.txt", 'r');
line = fstream.read().split("\n")
while line:
print(line);
line = fstream.read().split("\n")
fstream.close()
However my program diged into infinite loop.
picture of infinite loop
To solve this problem what should I do?
The result I expected is the following.
start.
hello world.
hello python.
I am studying file I/O in python
end.
The print function will automatically add a new line character. So
print msg
will print content of variable msg followed by a new line
If you do not want python to print the trailing new line, you have to add a comma to the end. This will print msg without the trailing newline. And if msg already has a new line which is the case when reading new lines from a file, you will see a single new line in place of double new lines.
print msg,
If you are using python 3 where print is called as a function, you can specify the end argument. See https://docs.python.org/3/library/functions.html#print
print(msg, end = '')
First of all, use a with statement to open the file so that you don't need to close it explicitly. Second, don't use a while loop for this; you can iterate over a file directly. Third, use the rstrip method to remove any trailing whitespace from line you read (or rstrip('\n') to remove only trailing newlines):
with open("input2.txt", 'r') as fstream:
for line in fstream:
line = line.rstrip('\n')
print(line)
In addition to the answers above; You can also use .splitlines()
fstream = open("input2.txt", 'r');
line = fstream.readline().splitlines()
while line:
print(line[0]);
line = fstream.readline().splitlines()
fstream.close()

python fileinput line endings

I've got some python code which is getting line endings all wrong:
command = 'svn cat -r {} "{}{}"'.format(svn_revision, svn_repo, svn_filename)
content = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read()
written = False
for line in fileinput.input(out_filename, inplace=1):
if line.startswith("INPUT_TAG") and not written:
print content
written = True
print line,
This fetches a copy of the file called svn_filename, and inserts the content into another file called out_filename at the "INPUT_TAG" location in the file.
The problem is the line endings in out_filename.
They're meant to be \r\n but the block I insert is \r\r\n.
Changing the print statement to:
print content, # just removes the newlines after the content block
or
print content.replace('\r\r','\r') # no change
has no effect. The extra carriage returns are inserted after the content leaves my code. It seems like something is deciding that because I'm on windows it should convert all \n to \r\n.
How can I get around this?
I can "solve" this problem by doing the following:
content = content.replace('\r\n', '\n')
converting the newlines to unix style so when the internal magic converts it again it ends up correct.
This can't be the right/best/pythonic way though....
CRLF = Carriage Return Line Feed.
Python on Windows makes a distinction between text and binary files;
the end-of-line characters in text files are automatically altered
slightly when data is read or written.
https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
Can you output as binary file and not as a text file?
If you prefix the string with r to open the file as raw, does this prevent extra \r in the output?

Categories