Reputation: 909
I have many lists which I need to concatenate and print into a text file with headers. I have simplified the problem in the code below. What I want in the output text file is each header will have a number underneath it, I would like to be able to write the concatenated list but when I do the text file shows the list with brackets and commas).
Code
import csv
headers = ["emplyee ID","age","number of fish", "number of eyes","tentacles","spots"]
listA = [1102,33,17]
listB = [3,8,19]
output_list = listA + listB
results_filename = "my_results.txt"
filepath = ("/path/to/results/folder/")
results_file_path = filepath + results_filename
with open(results_file_path, 'w', newline='') as filey:
csv_writer = csv.writer(filey, delimiter = '\t', lineterminator='\r\n')
csv_writer.writerow(["emplyee ID", "age", "number of fish", "number of eyes", "tentacles", "spots"])
filey.close()
with open(results_file_path, 'a', newline='') as filey: ## Python 3... with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
csv_writer = csv.writer(filey, delimiter = '\t', lineterminator='\r\n')
#csv_writer.writerow([output_list])
csv_writer.writerow([output_list[:]])
Output
The contents of the list to be written to the text file so that the commas and brackets are not present and it can be read using the tab deliver.
Upvotes: 0
Views: 659
Reputation: 44858
writerow
can write any iterable of integers:
A row must be an iterable of strings or numbers for
Writer
objects
...so you can write the list directly:
csv_writer.writerow(output_list)
Upvotes: 1