Question

I am writing a short script to read in 1000 names from a text file, with each name on its own line. I need to make each name capital, then pad an asterisk onto the beginning and the end. Like * JOHN DOE *. What happens is that when it reads in the line it keeps the formatting, so it jumps to the next line each time before the last asterisk can be added. Any thoughts? Thanks!

def main():
    infile=open("putNamesHere.txt","r")
    outfile=open("getFromHere.txt","w")
    for line in infile:
        line=line.upper()
        mystring=('*'+line+'*')
        outfile.write(mystring)
Was it helpful?

Solution

Remove newline from the string:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top