Posts: 1,211 Threads: 146 Joined: Jul 2017 Sep-23-2017, 07:00 AM (This post was last modified: Sep-23-2017, 07:00 AM by Pedroski55.) 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? Posts: 1,298 Threads: 38 Joined: Sep 2016 Sep-23-2017, 01:08 PM (This post was last modified: Sep-24-2017, 12:05 AM by sparkz_alot.) 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 Posts: 1,211 Threads: 146 Joined: Jul 2017 Why does that not write 11 10 or mary as 1 word in 1 cell? Posts: 1,298 Threads: 38 Joined: Sep 2016 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 Posts: 7,398 Threads: 123 Joined: Sep 2016 Yes range(len(sequence)) is not so popular >>> 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 Posts: 1,211 Threads: 146 Joined: Jul 2017 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) Posts: 1,298 Threads: 38 Joined: Sep 2016 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 |