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)
4
  • 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. Commented Jan 8, 2020 at 15:25
  • @AliBeyit I've had a go but still no luck - any tips? Commented Jan 8, 2020 at 15:54
  • Share your code, what you did so I can help Commented Jan 8, 2020 at 15:56
  • @AliBeyit I used the with to add integers, as that has already used a for loop to separate the values? Commented Jan 8, 2020 at 15:58

2 Answers 2

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.

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

6 Comments

just tried this and I'm getting 'TypeError: 'newline' is an invalid keyword argument for this function'
Just remove it, it is an optional parameter.
Hey, now I'm getting 'TypeError: writerows() argument must be iterable'. What is going on!
Bro.... just breathe... and know what TypeError is... You mistyped writerow...
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 ;)
|
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.

7 Comments

still no luck :( Also my code really isn't liking newline=''
The newline='' worked on my machine, but it seems it can also create problems; suggestions in this link say it's ok to use "wb" without the newline parameter.
If you delete the newline parameter, do you still have errors? Can you tell me what they are?
@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.
@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.
|

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.