To write Python lists to columns in a CSV (Comma-Separated Values) file, you can use the built-in csv module in Python. Here's an example of how to do this:
import csv # Example lists column1 = [1, 2, 3, 4] column2 = ['A', 'B', 'C', 'D'] # Specify the CSV file name csv_file = 'output.csv' # Combine the lists into rows rows = zip(column1, column2) # Write the data to the CSV file with open(csv_file, 'w', newline='') as file: writer = csv.writer(file) # Write a header row (optional) writer.writerow(['Column1', 'Column2']) # Write the data rows writer.writerows(rows) print(f'Data has been written to {csv_file}.') In this example:
We import the csv module.
We define two example lists, column1 and column2.
We specify the name of the CSV file we want to create or overwrite (output.csv in this case).
We use the zip() function to combine the lists into rows. Each tuple in the rows variable represents one row of data.
We open the CSV file in write mode ('w') and use csv.writer to create a writer object.
We optionally write a header row using writer.writerow() to specify column names.
We write the data rows using writer.writerows() to write the rows of data to the CSV file.
After running this code, you'll have a CSV file named output.csv with two columns containing the data from column1 and column2.
Make sure to replace column1 and column2 with your own lists, and adjust the file name and header row as needed.
Writing Python Lists to CSV as Separate Columns
import csv # Data to write as separate columns names = ['Alice', 'Bob', 'Charlie', 'David'] ages = [25, 30, 35, 40] with open('output.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Name', 'Age']) # Write column headers for name, age in zip(names, ages): writer.writerow([name, age]) # Write list data as separate columns Writing Multiple Lists of Same Length to CSV Columns
import csv cities = ['New York', 'Los Angeles', 'Chicago', 'Houston'] populations = [8419600, 3980400, 2716000, 2328000] with open('cities.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(['City', 'Population']) # Write headers for city, population in zip(cities, populations): writer.writerow([city, population]) # Write data to columns Writing Python Lists to Specific Columns in CSV
import csv fruits = ['Apple', 'Banana', 'Cherry', 'Date'] prices = [1.2, 0.5, 3.0, 2.5] with open('fruits.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Fruit', 'Price']) # Column headers for fruit, price in zip(fruits, prices): writer.writerow([fruit, price]) # Write lists to specific columns Writing Python Lists to CSV Columns with Custom Delimiter
import csv first_names = ['John', 'Jane', 'Jack', 'Jill'] last_names = ['Doe', 'Smith', 'Brown', 'Johnson'] with open('names.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile, delimiter=';') # Custom delimiter writer.writerow(['First Name', 'Last Name']) for first_name, last_name in zip(first_names, last_names): writer.writerow([first_name, last_name]) # Write data with custom delimiter Appending Data to CSV Columns from Python Lists
import csv additional_names = ['Eve', 'Frank'] additional_ages = [28, 32] with open('output.csv', 'a', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) for name, age in zip(additional_names, additional_ages): writer.writerow([name, age]) # Append new data to existing CSV Writing Python Lists to CSV with Header from Variable
import csv # Lists of data colors = ['Red', 'Blue', 'Green', 'Yellow'] shades = ['Light', 'Dark', 'Bright', 'Pastel'] # Generate a dynamic header headers = ['Color', 'Shade'] with open('colors.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(headers) # Write dynamic header for color, shade in zip(colors, shades): writer.writerow([color, shade]) # Write lists to CSV columns Writing Lists to CSV with Mixed Data Types in Columns
import csv names = ['Sam', 'Max', 'Tom', 'Ben'] scores = [85, 92, 78, 94] with open('scores.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Name', 'Score']) # Column headers for name, score in zip(names, scores): writer.writerow([name, score]) # Write mixed data types to CSV columns Writing Python Lists to CSV with Line Breaks in Columns
import csv tasks = ['Task 1', 'Task 2\nSubtask 2.1', 'Task 3', 'Task 4\nSubtask 4.1'] statuses = ['Completed', 'In Progress', 'Pending', 'In Progress'] with open('tasks.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Task', 'Status']) # Column headers for task, status in zip(tasks, statuses): writer.writerow([task, status]) # Write multi-line data to CSV columns Writing Python Lists to CSV Columns with Custom Formatting
import csv products = ['laptop', 'smartphone', 'tablet', 'desktop'] prices = [999.99, 799.99, 499.99, 1199.99] with open('formatted_products.csv', 'w', newline='', encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Product', 'Price']) for product, price in zip(products, prices): writer.writerow([product.capitalize(), f"${price:.2f}"]) # Apply custom formatting popover aspose tidyverse miniconda css-multicolumn-layout extrafont dyld windows-10-universal namevaluecollection del