0

I am simply trying to create csv file from lists. Here's the sample code:

import csv

def funcc(data):
    with open("sample.csv", "wb") as f:
        writer = csv.writer(f) 
        writer.writerows(data)

if __name__ == "__main__":
    data = [['id', 'name', 'score'], 
            ['1', 'john', '2332'],
            ['2', 'ned', '1213'],
            ['3', 'rob', '8343']]
    funcc(data)

This creates the output but everything is in one column. I tried using csv.writer(f, delimiter=',') and csv.writer(f, dialect='excel'), as mentioned in many SO answers, but nothing seems to work.

The output remains the same:all items in single column

Is there something I am missing or not doing right??

3
  • CSV means simply "Comma Separated". You could achieve all this very easily without the csv module, using Python's "native" file operations. Commented Dec 2, 2017 at 10:35
  • yes correct, turned out it was just simple piece of detail about I was missing opening it in 'LibreOfffice'. Format is right everything is right, I could not see the intended output, so I guessed something was wrong. Commented Dec 2, 2017 at 10:45
  • 1
    If you are using Python 2.x, your file should be opened with 'wb' not just 'w'. Commented Dec 2, 2017 at 11:38

1 Answer 1

1

There is a dialog window with import settings when you open this file in libreoffice. Try to change separator and it should be fine.

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

1 Comment

thank you for informing, missed that piece of detail, I can see more clear now! :D

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.