Python text file strings retain formatting

I am writing a short script to read 1000 names from a text file, with each name on its own line. I need to make every capital name, and then put an asterisk at the beginning and end. Like * JOHN DE. *. It happens that when he reads in a line, he retains the formatting, so every time before the last asterisk is added, it will go to the next line. Any thoughts? Thank you

def main(): infile=open("putNamesHere.txt","r") outfile=open("getFromHere.txt","w") for line in infile: line=line.upper() mystring=('*'+line+'*') outfile.write(mystring) 
+4
source share
1 answer

Remove the line from line to line:

 def main(): infile=open("putNamesHere.txt","r") outfile=open("getFromHere.txt","w") for line in infile: line=line.upper() mystring=('*'+line.rstrip()+'*\n') outfile.write(mystring) 
+4
source

Source: https://habr.com/ru/post/1496031/


All Articles