Reputation: 1
I am trying to make it so I can compare what the user enters as name and space with what is already on the database. If it is there it doesn't enter the users name and space, but if it isn't there it does enter the users name and space on a new row. But I cant get it to work.
This is my code so far:
name = entry2.get()
space = entry3.get()
file1 = open("database.csv", 'r')
lines = file1.read()
with open("database.csv", 'a+', newline='') as fp:
if name or space in lines:
print("No")
elif:
writer = csv.writer(fp)
writer.writerow([name, '9:00am-10:00am', space])
Any help would be much appreciated.
Upvotes: 0
Views: 28
Reputation: 11
Since you are dealing with a CSV file, I recommend using the Pandas library. With it, you can read the CSV file into a DataFrame which is pretty easy to manipulate.
import pandas as pd
file1_df = pd.read_csv('database.csv')
# if the columns in your csv are also called name and space
if name in file1_df.name or space in file1_df.space:
print('No')
else:
new_row = {'name': name, 'time': '9:00am-10:00am', 'space': space}
file1_df = file1_df.append(new_row, ignore_index=True)
file1_df.to_csv('database.csv', index=False)
Upvotes: 1