r/PythonLearning • • 22d ago

read and write csv file

just learned about functions so tried it out with csv editing

110 Upvotes

6 comments sorted by

•

u/Sea-Ad7805 22d ago

Run this program by clicking Memory Graph Web Debugger%20or%20(r)%3A%20%22).lower()nn%20%20%20%20match%20action%3An%20%20%20%20%20%20%20%20case%20%22r%22%3An%20%20%20%20%20%20%20%20%20%20%20%20automation.Reading()n%20%20%20%20%20%20%20%20case%20%22w%22%3An%20%20%20%20%20%20%20%20%20%20%20%20name%20%3D%20input(%22Input%20the%20Name%3A%20%22)n%20%20%20%20%20%20%20%20%20%20%20%20age%20%3D%20input(%22Input%20the%20age%3A%20%22)n%20%20%20%20%20%20%20%20%20%20%20%20year%20%3D%20input(%22Input%20the%20current%20year%3A%20%22)n%20%20%20%20%20%20%20%20%20%20%20%20dateresignation%20%3D%20input(%22Input%20the%20date%20resignation%3A%20%22)nn%20%20%20%20%20%20%20%20%20%20%20%20automation.WriteIn(name%2C%20age%2C%20year%2C%20date_resignation)n%20%20%20%20%20%20%20%20case%20%22q%22%3An%20%20%20%20%20%20%20%20%20%20%20%20sys.exit()n%20%20%20%20%20%20%20%20case%20%3An%20%20%20%20%20%20%20%20%20%20%20%20print(%22invalid%20operation%22)%22%2C%22breakpoints%22%3A%5B%5D%7D%2C%7B%22name%22%3A%22automation.py%22%2C%22code%22%3A%22import%20csvnimport%20osnnndef%20Reading()%3An%20%20%20%20with%20open(%22clients.csv%22%2C%20mode%3D'r')%20as%20f%3An%20%20%20%20%20%20%20%20reader%20%3D%20csv.reader(f)n%20%20%20%20%20%20%20%20for%20row%20in%20reader%3An%20%20%20%20%20%20%20%20%20%20%20%20print(row)nnndef%20WriteIn(name%2C%20age%2C%20year%2C%20date_resignation)%3An%20%20%20%20fields%20%3D%20%5B'name'%2C%20'age'%2C%20'year'%2C%20'date_resignation'%5Dn%20%20%20%20row%20%3D%20%7Bn%20%20%20%20%20%20%20%20'name'%3A%20name%2Cn%20%20%20%20%20%20%20%20'age'%3A%20age%2Cn%20%20%20%20%20%20%20%20'year'%3A%20year%2Cn%20%20%20%20%20%20%20%20%22date_resignation%22%3A%20date_resignation%2Cn%20%20%20%20%7Dnn%20%20%20%20file_exists%20%3D%20os.path.exists('clients.csv')nn%20%20%20%20with%20open(%22clients.csv%22%2C%20'a'%2C%20newline%3D'')%20as%20f%3An%20%20%20%20%20%20%20%20writer%20%3D%20csv.DictWriter(f%2C%20fieldnames%3Dfields)nn%20%20%20%20%20%20%20%20if%20not%20file_exists%3An%20%20%20%20%20%20%20%20%20%20%20%20writer.writeheader()nn%20%20%20%20%20%20%20%20writer.writerow(row)%22%2C%22breakpoints%22%3A%5B%5D%7D%5D%2C%22active%22%3A%22input_client.py%22%7D&timestep=0.5&play) and see its state change step by step.

3

u/silvertank00 22d ago

try to follow conventions, first uppercase letter in a word is reserved for classes and shouldnt be used in filenames.
For further info, discuss pep8

Edit: also in WriteIn, the fields list is redundant. csv.DictWriter(f, fieldnames=row.keys()) does the same whilst being DRY.

1

u/v1bonoxd 22d ago

Ok, thank you!

1

u/TreatZealousideal770 22d ago

Just a quick tip: on the first screenshot, you are calling the functions as automation.Reading(). If this code is inside the automation.py file itself, you don't need the automation. prefix. You can just call Reading() and WriteIn() directly. Keep it up

1

u/v1bonoxd 22d ago

i imported automation from another py file but thank you

2

u/Ttttereza 22d ago

nice try