Home > Enterprise >  Extra blank line is getting printed at the end of the output in Python
Extra blank line is getting printed at the end of the output in Python

Time:11-11

I am trying to read a file from command line and trying to replace all the commas in that file with blank. Below is my code:

import sys
datafile = sys.argv[1];

with open(datafile, 'r') as data:
  plaintext = data.read()

plaintext = plaintext.replace(',', '')
print(plaintext)

But while printing the plaintext I am getting one extra blank row at the end. Why is it happening and how can I get rid of that?

CodePudding user response:

You might be able to use

plaintext.rstrip('\n') 

This should remove the extra line

  • Related