Skip to content

Instantly share code, notes, and snippets.

@altairlage
Created November 27, 2020 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altairlage/96b2f74222f1a614830330bf8e982f4b to your computer and use it in GitHub Desktop.
Save altairlage/96b2f74222f1a614830330bf8e982f4b to your computer and use it in GitHub Desktop.
import csv
from prettytable import from_csv
def main():
write_csv()
read_csv()
import_pt_from_csv()
def write_csv():
with open('test.csv', 'w', newline='') as csv_file:
fieldnames = ['emp_name', 'dept', 'birth_month']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'emp_name': 'John Smith', 'dept': 'Accounting', 'birth_month': 'November'})
writer.writerow({'emp_name': 'Erica Meyers', 'dept': 'IT', 'birth_month': 'March'})
def read_csv():
with open('test.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['emp_name'], row['dept'], row['birth_month'])
def import_pt_from_csv():
with open("test.csv") as fp:
mytable = from_csv(fp)
print(mytable.get_string())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment