The Student Room Group

Python help

the code is meant to save the name of the volunteer onto a textfile every time the code runs. the names come all in one line but I want each name to appear on a different line in the textfile.
Does anyone know how I can make each name appear on a different line


n_ame= input("ENTER YOUR NAME: ")
print ("hello", n_ame)
file= open("coincount.txt" , "a")
file.write("volunteer name:" + n_ame)
file.close()
Reply 1
Original post by srjswnk
the code is meant to save the name of the volunteer onto a textfile every time the code runs. the names come all in one line but I want each name to appear on a different line in the textfile.
Does anyone know how I can make each name appear on a different line


n_ame= input("ENTER YOUR NAME: ")
print ("hello", n_ame)
file= open("coincount.txt" , "a")
file.write("volunteer name:" + n_ame)
file.close()

Are you saying there is a single input file containing ALL the names on one line?

In that case your first task is to split the line of data into separate names - are the names separated by commas or some other delimiter? Look up csv file processing as a start.
Reply 2
Add +'\n' to where you are writing to the file. That can be a quick fix.
Reply 3
Original post by davros
Are you saying there is a single input file containing ALL the names on one line?

In that case your first task is to split the line of data into separate names - are the names separated by commas or some other delimiter? Look up csv file processing as a start.

yes all the names are on one line, in one textfile, but i need each name to appear on a new line
Reply 4
Original post by srjswnk
yes all the names are on one line, in one textfile, but i need each name to appear on a new line

Sorry I think i misinterpreted and thought you were reading in the names, In that case , the suggestion above about writing a newline character '\n' should work for writing the file.

Quick Reply

Latest

Trending

Trending