Let's say I have this code:
from prettytable import PrettyTable
f = open("test.txt", "w")
t = PrettyTable()
def main():
animal = input("Enter an animal: ")
car = input("Enter a car: ")
column_names = ["animal", "car"]
t.add_column(column_names[0], [animal])
t.add_column(column_names[1], [car])
table_txt = t.get_string()
with open("test.txt", "w") as file:
file.write(table_txt)
def append():
shoe = input("Enter a shoe: ")
table_txt = t.get_string()
with open("test.txt", "a") as file:
file.write(table_txt)
cnt = input("Are you appending, or submitting new data? A for APPEND, N for NEW: ")
if cnt == 'N':
main()
else:
if cnt == 'A':
append()
f.close()
When I first write to the file, I type "N" when asked to create a new table, and enter "cat" for animal, and "Honda" for car and get this result:
+--------+-------+
| animal | car |
+--------+-------+
| cat | Honda |
+--------+-------+
If I wanted to append data (shoe) to the file, I would select "A" when asked, and enter a type of shoe. How can I append this data to the table as a new column, without created a whole new table?
**Update: