Python Printing Quotation Marks on Two Lines Instead of One - python

I am reading from an external text file, named 'greeting.txt' where the contents of the text file are simply:
HELLO
However, when I attempt to print the contents of the text file enclosed in quotes the terminal prints out:
"HELLO
"
I am using the following code:
for line in open('greeting.txt', "r"): print ('"%s"' % line)
I desire the string to be enclosed in quotes printed on the same line.
I have never encountered this problem before despite using Python for similar purposes, any help would be appreciated.

There is a end of line character in your text file after Hello. That end of line is also getting enclosed in the double quotes and causing the second quote to get printed on the second line. You should strip the end of line using rstrip()
for line in open('greeting.txt', "r"): print ('"%s"' % line.rstrip())

The problem is that, what is written in your file is probably Hello\n and if you read the whole line you are then printing "Hello\n" which causes the newline to be in front of the second quote. Use the strip() method to get rid of any trailing whitespaces like so:
for line in open('greeting.txt', "r"): print ('"%s"' % line.strip())
However I would suggest changing your code to:
with open('greeting.txt', "r") as f:
for line in f: print ('"%s"' % line.strip())
Since I personally do not like to have open without making sure, that the file is being closed as soon as I am done with it.

You can strip the trailing whitespaces using the rstrip() function.
for line in open('greeting.txt', "r"): print ('"%s"' % line.rstrip())

Related

How to write to a file with newline characters and avoid empty lines

I'm trying to write encoded data to a file and separate each run with a newline character. However, when doing this there is an empty line between each run -- as shown below.
Using .rstrip()/.strip() only really works when reading the file -- and obviously this cannot be used directly when writing to the file as it would write all the data to a single line.
cFile = open('compFile', 'w')
for i in range(num_lines):
line = validLine()
compressedFile.write(line + "\n")
cFile.close()
cFile = open('compFile', 'r')
for line in cFile:
print(line)
# Empty space output:
023
034
045
# Desired output:
023
034
045
I think you already did what you want if you have a look at your text file.
Be aware, that python reads the \n at the end of your file too and that print() makes a newline at the end of the printed line.
In your case that means your file should look like
023\n
034\n
045\n
When printing, you at first read 023\n and then as python does with the print() function you append a \n to your line.
Then you have the 023\n\n you get in your console. But in the file you have what you want.
If you just want to print without linebreak, you can use
import sys
sys.stdout.write('.')
You could use
for i in range(num_lines):
line = validLine()
compressedFile.write(line.strip() + "\n")
# ^^^
cFile.close()
Off-topic but consider using with () additionally.
Using .rstrip()/.strip() only really works when reading the file -- and obviously this cannot be used directly when writing to the file as it would write all the data to a single line.
This is a misconception. Using .rstrip() is exactly the correct tool if you need to write a series of strings, some of which may have a newline character attached:
with open('compFile', 'w') as cFile:
for i in range(num_lines):
line = validLine().rstrip("\n") # remove possible newline
compressedFile.write(line + "\n")
Note that if all your lines already have a newline attached, you don't have to add more newlines. Just write the string directly to the file, no stripping needed:
with open('compFile', 'w') as cFile:
for i in range(num_lines):
line = validLine() # line with "\n" newline already present
compressedFile.write(line) # no need to add a newline anymore
Next, you are reading lines with newlines from your file and then printing them with print(). By default, print() adds another newline, so you end up with double-spaced lines; your input file contains 023\n034\n045\n, but printing each line ('023\n', then '034\n', then '045\n') adds a newline afterwards and you write out 023\n\n034\n\n045\n\n out to stdout.
Either strip that newline when printing, or tell print() to not add a newline of its own by giving it an empty end parameter:
with open('compFile', 'r') as cFile:
for line in cFile:
print(line, end='')

Why is a new line being created when concatenating strings

I'm trying to open a file, and edit a specific line. When I concatenate a character onto one of the lines, it works, but inserts a new line. However I don't want a new line. Here is the code:
def moveCurlyInline(line, i):
with open('test.js', 'r') as inputFile:
data = inputFile.readlines()
print(data[0])
print(data[0] + ' {')
The print outputs:
function hello()
then:
function hello()
{
I need the curly bracket to be on the same line as the function hello. Any idea what's wrong with my code?
f.readline() reads a line from the file, including the newline at the end of the line.
Try stripping the extra newline:
data = [line.rstrip("\n") for line in inputFile]
You can strip new line character by
inputFile.read().striplines()

Python Remove Whitespace

This is my first question so forgive me if this is not a well formed question but I am trying to read the contents of a file in Python.
So far I could print the contents of the file but there are whitespaces at the beginning and end of each line and I don't want the whitespaces on the beginning. How do I do that.
with open('dump.txt','r') as f:
print f.read()
Thanks!
You can do something like this.
with open('dump.txt','r') as f:
for line in f:
print line.lstrip()
lstrip specifically removes the whitespace from the beginning of the string.
PS. read gives you the whole content of the file, you better operate on the line level by readline
UPDATE:
As pointed out, there are severals ways of doing this and one other way is to read the contents of the file via readlines and iterate through that list to strip the whitespace.
this reads the file line for line and left-strips each line
with open('dump.txt','r') as file:
for line in file:
print line.lstrip()
To cut off trailing or leading white spaces you can do
>>' Test '.lstrip()
'Test '
or
>>'Test '.rstrip()
'Test '
or
>>'Test '.strip()
'Test'

Python is adding extra newline to the output

The input file: a.txt
aaaaaaaaaaaa
bbbbbbbbbbb
cccccccccccc
The python code:
with open("a.txt") as f:
for line in f:
print line
The problem:
aaaaaaaaaaaa
bbbbbbbbbbb
cccccccccccc
as you can see the output has extra line between each item.
How to prevent this?
print appends a newline, and the input lines already end with a newline.
A standard solution is to output the input lines verbatim:
import sys
with open("a.txt") as f:
for line in f:
sys.stdout.write(line)
PS: For Python 3 (or Python 2 with the print function), abarnert's print(…, end='') solution is the simplest one.
As the other answers explain, each line has a newline; when you print a bare string, it adds a line at the end. There are two ways around this; everything else is a variation on the same two ideas.
First, you can strip the newlines as you read them:
with open("a.txt") as f:
for line in f:
print line.rstrip()
This will strip any other trailing whitespace, like spaces or tabs, as well as the newline. Usually you don't care about this. If you do, you probably want to use universal newline mode, and strip off the newlines:
with open("a.txt", "rU") as f:
for line in f:
print line.rstrip('\n')
However, if you know the text file will be, say, a Windows-newline file, or a native-to-whichever-platform-I'm-running-on-right-now-newline file, you can strip the appropriate endings explicitly:
with open("a.txt") as f:
for line in f:
print line.rstrip('\r\n')
with open("a.txt") as f:
for line in f:
print line.rstrip(os.linesep)
The other way to do it is to leave the original newline, and just avoid printing an extra one. While you can do this by writing to sys.stdout with sys.stdout.write(line), you can also do it from print itself.
If you just add a comma to the end of the print statement, instead of printing a newline, it adds a "smart space". Exactly what that means is a bit tricky, but the idea is supposed to be that it adds a space when it should, and nothing when it shouldn't. Like most DWIM algorithms, it doesn't always get things right—but in this case, it does:
with open("a.txt") as f:
for line in f:
print line,
Of course we're now assuming that the file's newlines match your terminal's—if you try this with, say, classic Mac files on a Unix terminal, you'll end up with each line printing over the last one. Again, you can get around that by using universal newlines.
Anyway, you can avoid the DWIM magic of smart space by using the print function instead of the print statement. In Python 2.x, you get this by using a __future__ declaration:
from __future__ import print_function
with open("a.txt") as f:
for line in f:
print(line, end='')
Or you can use a third-party wrapper library like six, if you prefer.
What happens is that each line as a newline at the end, and print statement in python also adds a newline. You can strip the newlines:
with open("a.txt") as f:
for line in f:
print line.strip()
You could also try the splitlines() function, it strips automatically:
f = open('a.txt').read()
for l in f.splitlines():
print l
It is not adding a newline, but each scanned line from your file has a trailing one.
Try:
with open ("a.txt") as f:
for line in (x.rstrip ('\n') for x in f):
print line

Python: Prevent fileinput from adding newline characters

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?
Your newlines are coming from the print function
use:
import sys
sys.stdout.write ('some stuff')
and your line breaks will go away
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.
Instead of this:
print line # Put back line into file
use this:
print line, # Put back line into file
Change the first line in your for loop to:
line = line.rstrip().replace("findStr", "replaceStr")
Due to every iteration print statement ends with newline, you are getting blank line between lines.
To overcome this problem, you can use strip along with print.
import fileinput
def fixFile(fileName):
for line in fileinput.FileInput(fileName, inplace=1):
line = line.replace("findStr", "replaceStr")
print line.strip()
Now, you can see blank lines are striped.
For the update on Python 3.4, you can just use:
print(line, end = '')
to avoid the insertion of a new line.

Categories