1

I am trying to write a list of float or int into a csvfile row-wise.

for some reason I am able to write them if the list are made up of strings

import csv
even_numbers = ['2','4','6','8'] # list of strings
header = ['even numbers']

with open('even_numbers.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(header)
    writer.writerows(even_numbers)

but when there are integers it throws an error

import csv
even_numbers = [2,4,6,8] # list of integers
header = ['even numbers']

with open('even_numbers.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(header)
    writer.writerows(even_numbers)

error

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-15-ae817296f34e> in <module>
      6     writer = csv.writer(csvfile)
      7     writer.writerow(header)
----> 8     writer.writerows(Even_list)

Error: iterable expected, not int

What other ways can I write a list to csv row-wise?

8
  • sorry @wpercy it is supposed to be even_numbers Commented Dec 2, 2021 at 16:32
  • @Horla.li rerun your sample code given here and modify your question to show the correct result. You may need to use writer.writerows([even_numbers]) Commented Dec 2, 2021 at 16:34
  • 1
    So, writerows() is expecting a list of lists, but even_numbers is just a list when based on ints BUT it looks like a list of lists when based on strings (as a string can be iterated over). Commented Dec 2, 2021 at 16:34
  • Did you try to find a solution before asking your question? stackoverflow.com/questions/41585078/… Commented Dec 2, 2021 at 16:34
  • @RufusVS doing writer.writerows([even_numbers]) does not writer the row wise. it rather writes the list in one long column Commented Dec 2, 2021 at 16:36

2 Answers 2

2

The writerows() method expects a list of lists. A row is a list and the rows are a list of those.

Two solutions, depending on what you want:

even_numbers = [[2, 4, 6, 8]]

or

even_numbers = [[2], [4], [6], [8]]

To do that latter transformation automatically, use:

rows = [[data] for data in even_numbers]
writer.writerows(rows)
Sign up to request clarification or add additional context in comments.

1 Comment

If the first is what was wanted, probably better just to use writeRow() instead
1

According to the documentation, the writeRows method expects a list of row objects as a parameter.

You can also see the definition of a row object here.

That is:

A row must be an iterable of strings or numbers for Writer objects and a dictionary mapping fieldnames to strings or numbers (by passing them through str() first) for DictWriter objects.

The reason you are getting this message is that it is trying to convert each int in that list as a row itself. The reason it works for strings is that a string itself is iterable. However I suspect you would get unexpected behavior if you tried out a multiple digit number in your list of strings like this:

even_numbers = ['24','45','66','82'] # list of strings

It will likely split out each of the digits as columns in the row, because it is reading the string in as a row object.

Comments

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.