I have this dataset here:
import csv
numbers = [111, 222, 333, 444]
And I want to be able to output the contents to a csv file like so with no header:
111
222
333
444
I've tried this:
with open('numbers.csv', 'a') as nums:
writer = csv.writer(nums, lineterminator='\n')
writer.writerow(numbers)
Output:
111,222,333,444
I know it's because I'm using writerow(), but I have no clue how to write to the csv file without making it so. I've tried nums.write() but again, no clue how to space new lines
CodePudding user response:
writer.writerow actually takes a list as an argument and interpretes it like a CSV row. So I suppose you are trying to create a new row for each element in your list. This is a way to do that:
import csv
numbers = [111, 222, 333, 444]
with open('numbers.csv', 'a') as nums:
writer = csv.writer(nums, lineterminator='\n')
for i in numbers: # iterate through your list
writer.writerow([i]) # write a row containing each element