Skip to content
Advertisement

python: csv.writer – commas between numbers in the output [closed]

i suspect that this is a simple answer, but i cannot figure out the answer. (I did give it due diligence)

i wrote a simple python program to identify prime numbers. the program is function, but i’m receiving strange results in the output. when i have it write a number with multiple digits, each number is comma separated; for example, 13 is added to the document as 1,3. I would like to have a comma after each full number (13,) and don’t want commas within the number (1,3 or 1,301). eventually, i want to have each number on its own row (one of the issue that i ran into in my g1 program is that the row became too long around 50mill ;-)

Any thoughts?

#!/bin/python3

import time
import os
import csv

folderLocation = "c:/notNow/"    

primeName = "primeNumbers.csv"
# notPrimeName = "noPrimeNumbers.csv"

primePath=folderLocation + primeName
# notPrimePath=folderLocation + notPrimeName

no=13
os.makedirs(folderLocation)
f = open(primePath, "w")
writer = csv.writer(f)
writer.writerow(str(no))

output: 1,3

Advertisement

Answer

writerow expects a sequence of items (e.g list). A string is just seen as a sequence of individual characters, try this instead:

writer.writerow([no])
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement