0

I'm generating numbers with 21 characters, I want to write each number generated on a csv file.

The output has spaces between each character of every number so instead of "001657618586303086816" I have "0 0 1 6 5 ... 1 6".

Here is my code:

def generate_numbers(n):
    f=open("data.csv","a",newline='')
    writer=csv.writer(f,delimiter=' ')

    for i in range(n):
        numb='0016' + str(randint(111111111111111,999999999999999)) + '16'
    
        writer.writerow(nib)
        
f.close()
martineau
  • 119,623
  • 25
  • 170
  • 301
MtSet
  • 27
  • 3
  • I tried messing with the writer, I noticed if i change the delimiter to "," I get the characters of each number separated by commas. So I thought the problem is the delimiter but it has to have size 1. Why would I do a str.split()? I f i want the characters connected? Not sure I understand this – MtSet Nov 17 '21 at 18:07
  • Apologies, I misunderstood your question, interpreted it exactly backwards. – G. Anderson Nov 17 '21 at 18:09
  • If you aren't using a delimiter at all, I question why you're using the `csv` module at all. You could just write it using the standard [file i/o interface](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) instead and just adding a `.csv` extension – G. Anderson Nov 17 '21 at 18:13
  • 1
    That does solve it! Thank you so much!! Im using csv module because it was what I know but i will look into the file i/o interface it probably makes more sense to use that – MtSet Nov 17 '21 at 18:20

1 Answers1

0

But, you are yourself adding the spaces, aren't you?

(f,delimiter=' ')

you have spaces between the single quotes

  • Yes u are right. I noticed that that was the problem but didnt know how o solve itt because i couldn't take the delimiter out – MtSet Nov 17 '21 at 18:21