Grammar/Environments Library List Standard Library

[Python] How to Write List to Csv [csv module, pandas]

This article shows how to write list to csv.

Use "csv" module, a standard library for csv manipulation in Python.

As a supplement, I also show how to write a list to csv using "pandas".

Contents

[ csv module ]

  • Basic usage of "csv module"
  • How to write list into new csv file
  • How to write list into existing csv file

[ pandas ]

  • How to write list into new csv file
  • How to write list into existing csv file

sponsored link

Basic usage of "csv module"

In the beginning, I show basic usage of "csv module".

The "csv module" is a standard library in Python.

Therefore, installation with "pip" is not required.

This is an example code.

import csv

path_csv = r'C:\***** any path *****\test.csv'

with open(path_csv, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['No.', 'Fluits', 'Price'])
    writer.writerow([1, 'Apple', 100])
    writer.writerow([2, 'Banana', 200])
    writer.writerow([3, 'Orange', 100])

This is the output csv data.

> path_csv = r'C:***** any path *****\test.csv'

Specify the path of the new csv file.

By prefixing a string literal with "r", you can create a raw string in Python.

"Raw string" treats the backslash character "\n" as a literal character, not newline.

> with open(path_csv, 'w', newline='') as file:

A new csv file is created and opened by using "with open statement".

The file is automatically closed.

Therefore, you don’t have to remember to use file.close( ).

The ‘w‘ within the open( ) statement tells Python to use ‘write’ mode.

The other main modes are as follows.

ModeDescription
rOpen a file for reading. (default)
wOpen a file for writing.
aOpen a file for appending.

The " newline='' " within the open( ) statement is an option to prevent extra line breaks.

If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

reference : https://www.python.org/

> writer = csv.writer(file)

By using the csv.writer( ) function, a writer object is created.

To write data to a csv file, use the methods of a writer object.

There are two methods for writing:
writerow( ) method, which writes line by line, and writerows( ) method, which writes multiple lines at once.

> writer.writerow(['No.', 'Fluits', 'Price'])

The writerow( ) method is used to write data.

sponsored link

[csv module]How to write list into new csv file

I show how to write list into new csv file.

This is an example code.

import csv

list_2D = [
    ['No.', 'Fluits', 'Price'],
    [1, 'Apple', 100],
    [2, 'Banana', 200],
    [3, 'Orange', 100]
]

path_csv = r'C:\***** any path *****\test.csv'

with open(path_csv, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(list_2D)

> writer.writerows(list_2D)

The writerows( ) method is used to write list to csv file.

[csv module]How to write list into existing csv file

I show how to write list into existing csv file.

This is an example code.

import csv

list_2D = [
    [4, 'Lemon', 100],
    [5, 'Mango', 1000]
]

path_csv = r'C:\***** any path *****\test.csv'

with open(path_csv, 'a', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(list_2D)

> with open(excel_path, 'a', newline='') as file:

The ‘a‘ within the open( ) statement tells Python to use ‘appending’ mode.

sponsored link

[pandas]How to write list into new csv file

As a supplement, I show how to write a list to new csv file using "pandas".

This is an example code.

import pandas as pd

list_2D = [
    ['No.', 'Fluits', 'Price'],
    [1, 'Apple', 100],
    [2, 'Banana', 200],
    [3, 'Orange', 100]
]

path_csv = r'C:\***** any path *****\test.csv'

df = pd.DataFrame(list_2D[1:], columns=list_2D[0])

df.to_csv(path_csv, index=False)

> df = pd.DataFrame(list_2D[1:], columns=list_2D[0])

The List is converted to the DataFrame.

"list_1[0]" is specified as columns.

> df.to_csv(path_csv, index=False)

The DataFrame is output to the csv file.

[pandas]How to write list into existing csv file

I show how to write list into existing csv file.

This is an example code.

import pandas as pd

list_2D = [
[4, 'Lemon', 100],
[5, 'Mango', 1000]
]

path_csv = r'C:\***** any path *****\test.csv'

df = pd.DataFrame(list_2D)

df.to_csv(path_csv, mode='a', index=False, header=False)

> df.to_csv(path_csv, mode='a', index=False, header=False)

By specifying mode = 'a', the list is output to csv in append mode.

sponsored link

-Grammar/Environments, Library, List, Standard Library
-, , ,