Python Forum
Why are these numbers broken into their digits?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why are these numbers broken into their digits?
#1
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?
Reply
#2
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Why does that not write 11 10 or mary as 1 word in 1 cell?
Reply
#4
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']
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
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
Reply
#6
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)
Reply
#7
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is copying and pasting a block now broken? WagmoreBarkless 2 1,403 May-05-2022, 05:01 AM
Last Post: WagmoreBarkless
  Why is copying and pasting a block now broken? WagmoreBarkless 1 1,241 May-04-2022, 11:40 PM
Last Post: Larz60+
  BrokenPipeError: [Errno 32] Broken pipe throwaway34 6 9,361 May-06-2021, 05:39 AM
Last Post: throwaway34
  Single digits seem to be greater than double digits Frosty_GM 4 3,521 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Python broken if moved to a different folder ecastrotns 3 2,469 Oct-26-2020, 10:53 PM
Last Post: ecastrotns
  STT: recognition connection failed: [Errno 32] Broken pipe GrahamBerends 0 5,085 Jul-18-2020, 11:00 PM
Last Post: GrahamBerends
  Python DateTime is broken 10OctNotOct10a1 8 4,767 Jan-03-2020, 07:54 AM
Last Post: snippsat
  Broken interpreter? fcktheworld587 1 2,274 Dec-26-2019, 08:29 PM
Last Post: snippsat
  I need a query to select the numbers that include 2 digits of 3 omidvakili 1 1,649 Sep-20-2019, 03:49 PM
Last Post: Yoriz
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,753 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020