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?

martineau
  • 119,623
  • 25
  • 170
  • 301
se7en
  • 671
  • 4
  • 18
  • sorry @wpercy it is supposed to be `even_numbers` – se7en Dec 02 '21 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])` – RufusVS Dec 02 '21 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). – JonSG Dec 02 '21 at 16:34
  • Did you try to find a solution before asking your question? https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python – defladamouse Dec 02 '21 at 16:34
  • @RufusVS doing `writer.writerows([even_numbers])` does not writer the row wise. it rather writes the list in one long column – se7en Dec 02 '21 at 16:36
  • @defladamouse I tried it but my list is not a list of tuples or list of lists, it is just this `even_numbers = [2,4,6,8]` so it does help too much – se7en Dec 02 '21 at 16:39
  • Why are you bothering to write the data in CSV format when there's only one item per row (i.e. nothing to separate)? – martineau Dec 02 '21 at 16:52
  • @Horla.li replace `even_numbers = [2,4,6,8]` with `even_numbers = [[2,4,6,8]]` and it's a list of lists. – defladamouse Dec 02 '21 at 17:20

2 Answers2

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)
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
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.

Aaron
  • 1,893
  • 16
  • 24