0

i have copied the values from list to csv file like

there are 2 lists say

list1 =['a','b','c','d','e','f','g','h','i','j','k','l','m','n']
list2 = ['1','2','3','4','5','6','7','8','9','10','11','12','13']

and i am writting the 2 lists to csv file using the below code

import csv
from itertools import izip
with open('output1.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(izip(list1, list2))

when i open output1.csv in linux after executing the above thing , it is empty

but when i print it , using print(open("output1.csv").read()) in python script is printing the contents of the csv file propery

can you please let me know why the file is empty when i opened it in linux

6
  • Why b flag in the file opening mode? Why bytes? Commented Jan 11, 2018 at 9:48
  • even if i remove b means if it is modified in script like below with open('output1.csv', 'w') as f: , then also not working Commented Jan 11, 2018 at 9:53
  • Is the code indentation correct? Or did you make a mistake in this question? Commented Jan 11, 2018 at 10:10
  • ya it is correct , in question it came like that , actually i am able to read the output through open("output1.csv").read() , but file is empty when opened in linux vi output1.csv Commented Jan 11, 2018 at 10:14
  • open("output1.csv")qualifies as opening the file. If you are unable to open it with vi maybe your vi installation is corrupt or you are not using it correctly. Commented Jan 11, 2018 at 10:18

1 Answer 1

1

Next time when you are stuck then please be more specific about the error and output you expect so we don't have to waste time assuming your expected output. Now coming to the your problem when i open output1.csv in linux after executing the above thing , it is empty I think you need to read up more about iterators and also about file operations. So running your script in my machine gave me this error which is pretty easy to understand.

TypeError: a bytes-like object is required, not 'str'

Python and it's errors are so readable isn't it !? This error was correct as python was expecting a bytes-like object as the error says! Change that to w+ ( and read more about permissions in the links given in the answer). Now this writer.writerows(izip(list1, list2)) has to understood clearly ( also provided in the links in this answer). When life gives you an iterator, you just iterate it! Grab a reference to iterate the iterator and you are good. Please read more about it.

import csv
from itertools import izip

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
list2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13']

with open('output1.csv', 'w+') as f:
    writer = csv.writer(f)
    writer.writerows([i for i in izip(list1, list2)]) # your homework to figure out what this means and does.

Let me know if this answer worked for you by accepting it!

Cheers!

Sign up to request clarification or add additional context in comments.

3 Comments

No again the csv files are empty , above solution dint help to solve the problem
What did you try actually ? Are there any errors ? What about helping me helping you with a traceback ?
No errors are there , i can see the output of file through readlines but the file is empty when i open though vi editor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.