Python Forum

Full Version: Appending data into a file in tab delimited format
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import datetime
from datetime import datetime
n = 5
while n > 0:
id = input("Please enter your ID: ")
file = open("C:\\Users\\abc\\def\\" "%s.txt" % id, "a")
name = input("Please provide your name: ")
Doctor = input("Please provide the name of the doctor you would like to visit: ")
AppointmentDate = input("Please provide the date you want to see the doctor: ")
AppointmentTime = input("Please provide the time you want to see the doctor: ")
EmailID = input("Please provide your email to confirm your appointment: ")
PhoneNum = input("Please provide your phone number to receive confirmation message: ")
file.write(id)

When I run the above code I am able to create file with the ID as its name but it is not appending data
into the text file.
I need to append the data into the text file in tab delimited format.
Each time I want to enter a new line of data it should go to the next line as below
The data should go only into the respective ID file when the user with that ID executes the code.
User with id ABCD will create ABCD file and the below lines should go to his file.
User with id EFGH will create EFGH file and the below lines should go to his file.

Ex: IDABCD JAMES JOLIE 9/9/2019 13:00 [email protected] 9090909090
IDABEE JIMMY JOHNNY 9/9/2019 11:00 [email protected] 9000009090

Assistance is appreciated.
thanks
Is this what do you need?

a = "absbdbs"
b = "dcfdss"
c = "edfsds"

line = "{}\t{}\t{}\n".format(a, b, c)

with open("foo.log", "a") as f:
    f.write(line)