I'm new to python I made this code which makes you input data that is Automatticly inputted into excel. The thing is when I run the program twice it overwrites the previous data so I need to know how to save it. this is the code I wrote
import xlsxwriter
workbook = xlsxwriter.Workbook('Book1.xlsx')
worksheet = workbook.add_worksheet()
name = input('your full name: ')
age = int(input('your age: '))
emaill = input('your email: ')
idontknow = (
['Name', name],
['Age', age],
['Email', emaill],
)
row = 0
col = 0
for item, cost in idontknow:
worksheet.write(row, col, item)
worksheet.write(row, col 1, cost)
row = 1
workbook.close()
any suggestion will help
CodePudding user response:
I would recommend using "csv" - https://docs.python.org/3/library/csv.html excel can read and save to csv format, and I find it easier to work with.
CodePudding user response:
I would recommend using the pandas library to perform the data manipulation. A code that performs the task you indicate would be the following:
import pandas as pd
#read the file and make a DataFrame
data = pd.read_excel('Book1.xlsx')
df = pd.DataFrame(data)
#get the data
name = input('your full name: ')
age = int(input('your age: '))
email = input('your email: ')
#get the last edited row
row = len(df)
#write the data
df.loc[row, 'name'] = name
df.loc[row, 'age'] = age
df.loc[row, 'email'] = email
#save the file
df.to_excel('Book1.xlsx',index=False)
Additionally, this code allows you to have headers for each column in your file, which is convenient in most situations.