how do i write my output to a csv in multiple columns in python

  • Last Update :
  • Techknowledgy :

You need to decide what columns you want to write out. As you've mentioned, you want var1 and var2 written to separate columns. You could achieve this using this:

import csv

with open('names.csv', 'w') as csvfile:
   fieldnames = ['var1', 'var2']
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)

   'var1': var1,
   'var2': var2

create a list containing the rows, then write them line by line using csv's writerows function. This is useful if your column has a couple of entries i.e. the key has many values

import csv
list_column = ["column_A", "column_B"]
column_A = ["var_1"]
column_B = ["var_2"]

#to create a list full of rows as the writerow
function reads data row - wise
for i in range(len(column_A)):
   list_temp = [column_A[i], column_B[i]]

with open(your_csv_file_name, 'w', newline = "") as entry:
   writer = csv.writer(entry)


Extending @CalebJ's answer, shorter version could be like this:

list_1 = ["Hello", "World", "Monty", "Python"]
list_2 = [1, 2, 3, 4]

file = open("columns.txt", "w")
writer = csv.writer(file)

for w in range(4):
   iterate through integers 0 - 3

writer.writerow([list_1[w], list_2[w]])


Suggestion : 2

file - CSV file where we want to write to,Suppose we want to write a CSV file with the following entries:,Suppose we want to write a CSV file (office.csv) with the following content:,CSV files with lineterminator

We are going to exclusively use the csv module built into Python for this task. But first, we will have to import the module as :

import csv

Suppose we want to write a CSV file with the following entries:

SN, Name, Contribution
1, Linus Torvalds, Linux Kernel
2, Tim Berners - Lee, World Wide Web
3, Guido van Rossum, Python Programming

Here's how we do it.

import csv
with open('innovators.csv', 'w', newline = '') as file:
   writer = csv.writer(file)
writer.writerow(["SN", "Name", "Contribution"])
writer.writerow([1, "Linus Torvalds", "Linux Kernel"])
writer.writerow([2, "Tim Berners-Lee", "World Wide Web"])
writer.writerow([3, "Guido van Rossum", "Python Programming"])

Example 3: Write CSV File Having Pipe Delimiter

import csv
data_list = [
   ["SN", "Name", "Contribution"],
   [1, "Linus Torvalds", "Linux Kernel"],
   [2, "Tim Berners-Lee", "World Wide Web"],
   [3, "Guido van Rossum", "Python Programming"]
with open('innovators.csv', 'w', newline = '') as file:
   writer = csv.writer(file, delimiter = '|')


SN | Name | Contribution
1 | Linus Torvalds | Linux Kernel
2 | Tim Berners - Lee | World Wide Web
3 | Guido van Rossum | Python Programming

Suggestion : 3

Let’s first create a new list object that we will append as a new column later on. Note that this list needs to have the same length as the number of rows in our CSV file.,For this task, we can apply the to_csv function as shown below. All we have to specify is the name of our data set (i.e. data) and the name of the CSV file that we want to create (i.e. data.csv).,In the next step, we can convert this updated pandas DataFrame to another CSV file as shown below:,However, you can also see that our data is shown with a comma separator in only one column of the CSV file. Let’s change this!

import pandas as pd # Import pandas library in Python
data = pd.DataFrame({
   'x1': range(1, 7),
   # Create pandas DataFrame 'x2': [3, 9, 7, 3, 1, 8],
   'x3': ['a', 'b', 'c', 'd', 'e', 'f'],
   'x4': range(15, 9, -1)
print(data) # Print pandas DataFrame
import os # Load os
os.chdir('C:/Users/Joach/Desktop/my directory') # Set working directory
data.to_csv('data1.csv') # Export pandas DataFrame
data.to_csv('data2.csv', # Export pandas DataFrame sep = ";")