write in a csv file in python and read it using the column name -


while have written code

import csv     open('persons.csv', 'w') csvfile:         filewriter = csv.writer(csvfile, delimiter=',',quotechar='|')         filewriter.writerow(['name', 'profession'])         filewriter.writerow(['derek', 'software developer'])         filewriter.writerow(['steve', 'software developer'])         filewriter.writerow(['paul', 'manager']) 

and getting result as

['name', 'profession'] [] ['derek', 'software developer'] [] ['steve', 'software developer'] [] ['paul', 'manager'] [] 

it leaving line in between. how resolve ???

and 1 more thing want read data csv using column name i.e.

import csv open('persons.csv') f:     reader = csv.reader(f)       name, profession in reader:         print(name, profession) 

--when run code shows error..

 name, profession in reader: valueerror: not enough values unpack (expected 2, got 0) 

please suggest how can work

csv.writer writes \r\n file default. use following open file if running windows

with open('persons.csv', 'w', newline='') csvfile: 

once have this, empty lines go away

the second issue due first. since had empty lines, reader has 0 items lines, , hence exception.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -