0

Why is this not writing to my data.csv file?

import csv

x = raw_input("Enter FON numbers (seperated by a space)")
integers = [int(i) for i in x.split()]

with open("data.csv", "wb") as f:
   writer = csv.writer(f)
   writer.writerows(integers)
KunduK
  • 32,888
  • 5
  • 17
  • 41
booooooky
  • 31
  • 6
  • 1
    Loop over the integers list you just created and in each iteration check if the current element is integer (to make sure it actually is) and then just write that value to an csv. If you don't know how to write or retrieve values from csv a quick google search is your answer. – Ali Beyit Jan 08 '20 at 15:25
  • @AliBeyit I've had a go but still no luck - any tips? – booooooky Jan 08 '20 at 15:54
  • Share your code, what you did so I can help – Ali Beyit Jan 08 '20 at 15:56
  • @AliBeyit I used the with to add integers, as that has already used a for loop to separate the values? – booooooky Jan 08 '20 at 15:58

2 Answers2

0
import csv
with open('myCsvFile.csv', 'w') as file:
    writer = csv.writer(file)
    for i in myList:
        writer.writerow(i)

Try something like this. It should work for your purpose.

Ali Beyit
  • 431
  • 1
  • 6
  • 19
  • just tried this and I'm getting 'TypeError: 'newline' is an invalid keyword argument for this function' – booooooky Jan 08 '20 at 16:04
  • 1
    Just remove it, it is an optional parameter. – Ali Beyit Jan 08 '20 at 16:05
  • Hey, now I'm getting 'TypeError: writerows() argument must be iterable'. What is going on! – booooooky Jan 08 '20 at 16:07
  • Bro.... just breathe... and know what TypeError is... You mistyped writerow... – Ali Beyit Jan 08 '20 at 16:08
  • so there are no errors coming up now but when I run this in PyCharm console and then check the data.csv file nothing has been input? – booooooky Jan 08 '20 at 16:14
  • 1
    The great thing about programming is you learn by doing mistakes and searching for what you didn't understand for. If someone tells you everything once you just understand for a moment but next time you do the same mistake. So do some research and figure out what's the problem. At least you don't have any errors now ;) – Ali Beyit Jan 08 '20 at 16:19
0

The function writerows expects instead of int numbers, iterables. Try the following:

import csv

x = input("Enter FON numbers (seperated by a space)")
integers = [[int(i)] for i in x.split()]

with open("data.csv", "w") as f:
   writer = csv.writer(f)
   writer.writerows(integers)

In case you want to open the file in binary mode, you need to encode your data (otherwise you will receive a TypeError: a bytes-like object is required, not 'str'; you will need to handle the encoding to bytes, as shown here.

sstegaru
  • 1
  • 1
  • still no luck :( Also my code really isn't liking `newline=''` – booooooky Jan 08 '20 at 16:25
  • The `newline=''` worked on my machine, but it seems it can also [create problems](https://stackoverflow.com/questions/32811992/python3-csv-writer-failing-exiting-on-error-typeerror-newline-is-an-invalid?rq=1); suggestions in this link say it's ok to use "wb" without the newline parameter. – sstegaru Jan 08 '20 at 16:33
  • If you delete the newline parameter, do you still have errors? Can you tell me what they are? – sstegaru Jan 08 '20 at 16:33
  • @AliBeyit you had the conversation for your code, and you can discuss improvements to your code there, but for the sake of coherency and clarity (it might not be the same error), I will continue the discussion in this thread. – sstegaru Jan 08 '20 at 16:43
  • @booooooky I just updated the answer to handle that TypeError you were having (basically it's because the file is opened in binary mode, but you were trying to write unencoded text to it, which defaulted to str). It should work now. – sstegaru Jan 08 '20 at 16:55
  • @sstegaru hey! no errors after deleting newline parameter, but nothing is being written to the data.csv file still :/ – booooooky Jan 08 '20 at 16:55
  • @booooooky can you try adding this after the last statement (indended, within the with block): `f.flush()` – sstegaru Jan 08 '20 at 17:01