How to write a list to CSV in Python
CSV files can store data in a table format. The data is a simple text file, each line of data is called a record, and each line can be separated by commas.
This article will discuss different ways to write a list to CSV in Python.
csv.writer()
Write a list to CSV using the method in Python
We start by importing csv
the module.
import csv
Suppose we write the following entries into a CSV file.
RollNo, Name, Subject
1, ABC, Economics
2, TUV, Arts
3, XYZ, Python
Now, the complete example code is as follows.
import csv
with open("students.csv", "w", newline="") as student_file:
writer = csv.writer(student_file)
writer.writerow(["RollNo", "Name", "Subject"])
writer.writerow([1, "ABC", "Economics"])
writer.writerow([2, "TUV", "Arts"])
writer.writerow([3, "XYZ", "Python"])
If you run the code above, students.csv
a file will be created in the current directory with the entries in it as they appear in the code. csv.writer()
The function will create writer()
the object and writer.writerow()
the command will insert the entries into the CSV file line by line.
Writing Lists to CSV Using Quotes in Python
In this method, we will see how to write quoted values in CSV file. The complete sample code is as follows.
import csv
list = [["RN", "Name", "GRADES"], [1, "ABC", "A"], [2, "TUV", "B"], [3, "XYZ", "C"]]
with open("studentgrades.csv", "w", newline="") as file:
writer = csv.writer(file, quoting=csv.QUOTE_ALL, delimiter=";")
writer.writerows(list)
Creates a file in the current directory studentgrades.csv
. csv.QUOTE_ALL()
The function uses double quotes around all entries and ;
separates them with the delimiter .
Output:
RN;"Name";"GRADES"
1;"ABC";"A"
2;"TUV";"B"
3;"XYZ";"C"
pandas
Write a list to CSV using the Python method
This method uses the Pandas library, which has a complete data frame. If you don’t have this library installed on your computer, you can use an online tool called Google Colab.
The complete example code is as follows.
import pandas as pd
name = ["ABC", "TUV", "XYZ", "PQR"]
degree = ["BBA", "MBA", "BSC", "MSC"]
score = [98, 90, 88, 95]
dict = {"name": name, "degree": degree, "score": score}
df = pd.DataFrame(dict)
df.to_csv("test.csv")
Output:
name degree score
0 ABC BBA 98
1 TUV MBA 90
2 XYZ BSC 88
3 PQR MSC 95
This method writes a Python list to a CSV file as a data frame.
NumPy
Write a list to a CSV file using the method in Python
This method uses NumPy
the library to save a list to a CSV file in Python. The complete example code is as follows.
import numpy as np
list_rows = [
["ABC", "COE", "2", "9.0"],
["TUV", "COE", "2", "9.1"],
["XYZ", "IT", "2", "9.3"],
["PQR", "SE", "1", "9.5"],
]
np.savetxt("numpy_test.csv", list_rows, delimiter=",", fmt="% s")
NumPy
The library's savetxt()
function will write a CSV file from a Python list.
Output:
ABC COE 2 9
TUV COE 2 9.1
XYZ IT 2 9.3
PQR SE 1 9.5
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert a list to a set in Python
Publish Date:2025/05/08 Views:144 Category:Python
-
This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values in sequence. Python list stores comma separated values between
Remove the first element from a list in Python
Publish Date:2025/05/08 Views:172 Category:Python
-
This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which
Convert a list to lowercase in Python
Publish Date:2025/05/08 Views:112 Category:Python
-
Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin
Remove all occurrences of an element from a Python list
Publish Date:2025/05/08 Views:69 Category:Python
-
In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element
Convert hex to bytes in Python
Publish Date:2025/05/08 Views:101 Category:Python
-
Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for
b in front of string in Python
Publish Date:2025/05/08 Views:53 Category:Python
-
This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v
How to Convert Integer to Binary in Python
Publish Date:2025/05/08 Views:130 Category:Python
-
This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,
How to convert an integer to bytes
Publish Date:2025/05/08 Views:77 Category:Python
-
Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in
How to convert bytes to int in Python
Publish Date:2025/05/08 Views:111 Category:Python
-
Bytes The data type has a numerical range of 0~255 (0x00~0xFF). A byte has 8 bits of data, that's why its maximum value is 0xFF. In some cases, you need to convert a byte or byte array to an integer for further data processing. Let's see ho