Python Forum

Full Version: Why are these numbers broken into their digits?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using Python 3.5.2 and its csv module. I want to write to a csv file. To begin with, I just create a list:

 # open a file outputFile = open(pathToFile + 'outputFile.csv', 'w') outputFileWriter = csv.writer(outputFile) # create a list just to test the writer cellContent = [] for h in range(0, maxRow):	cell = str(h)	cellContent = cellContent + [cell]
then, I want to write the list to a csv file:

# write the list to the rows just to test for h in range(1, maxRow):	outputFileWriter.writerow(cellContent[h])
This works. The numbers are written to the rows, but when the number has 2 digits, each digit appears in a separate column. I need all the numbers in column 1.

Edit: Also, if I use words, I still get each letter in a separate column:

cellContent = ['Peter', 'Paul', 'Mary', 'Jane'] for cell in cellContent:	outputFileWriter.writerow(cell) 11 9 9 9 outputFile.close()
Any tips or ideas?
Just a quick thought, but what about:

a_list = ['11', 'mary', '1', '10'] new_list = [] for item in range(len(a_list)):     new_list.extend(a_list[item]) for character in range(len(new_list)):     print(new_list[character])
Output:
1 1 m a r y 1 1 0
Probably not the best solution, but I'm in a bit of a rush. Sorry.
Why does that not write 11 10 or mary as 1 word in 1 cell?
First, note that I added empty brackets to 'new_list' on line 2.
Second, this only works if the list consists of strings.
Finally, I am probably making every Python coder groan by my my use of  for item in range(len(a_list)) and  for character in range(len(new_list)) . Be that as it may...

So the first loop takes the original list and breaks it into individual characters.  The second loop prints the list 1 character at a time.

If we add a print() statement after the first loop:
for item in range(len(a_list)):     new_list.extend(a_list[item]) print(new_list)
we get
Output:
['1', '1', 'm', 'a', 'r', 'y', '1', '1', '0']
Yes range(len(sequence)) is not so popular Wink
>>> a_list = ['11', 'mary', '1', '10'] >>> for  item in list(''.join(c for c in a_list)): ...     print(item)
Output:
1 1 m a r y 1 1 0
I think I have not explained myself adequately:

If we take the list: cellContent = ['Peter', 'Paul', 'Mary', 'Jane'] and I am using the python csv module: I would like the words 'Peter', 'Paul', 'Mary', 'Jane' each in column 1, not spread as individual letters in various columns. For instance 'Peter', with 5 letters, occupies 5 columns in row 1 when I open the resultant .csv file.

I was hoping each word would be in 1 row and 1 column. For some reason, python is reading 'Peter' as a list and placing each letter of 'Peter' in a separate column.

How to change that? Why does it not write 'Peter' together in 1 cell in 1 column?

for cell in cellContent: outputFileWriter.writerow(cell)
Still not sure what you are looking for, because you are ambiguous in the use of column, but I'll see what I can do.

If we take this code, which uses your 'for' statement:

import csv cellContent = ['Peter', 'Paul', 'Mary', 'Jane'] with open('csv_file.csv', 'w') as new_file:     writer = csv.writer(new_file)     for cell in cellContent:         writer.writerow(cell)
you end up with a file content of

Output:
P,e,t,e,r P,a,u,l M,a,r,y J,a,n,e
Not what you are looking for I take it, plus you have an extra line between rows. We can avoid the extra lines by adding newline='' to the open() function, and remove the commas using the split() function. The code now looks like:

import csv cellContent = ['Peter', 'Paul', 'Mary', 'Jane'] with open('csv_file.csv', 'w', newline='') as new_file:     writer = csv.writer(new_file)     for cell in cellContent:         writer.writerow(cell.split(','))
and the file looks like

Output:
Peter Paul Mary Jane