开发者

storing the output of a cursor object in csv format

开发者 https://www.devze.com 2023-01-07 14:42 出处:网络
i\'ve accessed a database and have the result in a cursor object. but i couldn\'t save it :( cur_deviceauth.execute(\'select * from device_auth\')

i've accessed a database and have the result in a cursor object. but i couldn't save it :(

cur_deviceauth.execute('select * from device_auth')  
fo开发者_Python百科r row in cur_deviceauth:  
    print row  
writer = csv.writer(open("out.csv", "w"))  
writer.writerows(cur_deviceauth)  

i don't get an error msg and i couldn't write it. how do i make it? any advice would be of much help and what is the best place to learn this stuff?


When you're printing rows before writing to a file, you're exhausting cursor object that works as a generator. Just write to file without any intermediate steps.

0

精彩评论

暂无评论...
验证码 换一张
取 消