Created
November 27, 2020 21:20
-
-
Save altairlage/96b2f74222f1a614830330bf8e982f4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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