Convert CSV file to JSON file in Python
This tutorial will demonstrate various ways to read data from a CSV file and save it as a JSON file in Python. In web applications, the format used to save and transfer data is the JSON format. Assume that we have saved the data in CSV (Comma Separated Values) format and need to convert it to JSON format.
Therefore, we need some method to convert the data in CSV format to JSON format. We can use the following method to convert CSV file to JSON file in Python.
json.dump()
Convert CSV file to JSON file using method in Python
json.dump(obj, fp, indent=None, Seperator=None)
The method takes data obj
as input and obj
serializes it into a JSON formatted stream and writes it to a file-like object fp
.
If we want to add indentation to the data to make it easier to read, we can use indent
the keyword argument. For indent
an argument value equal 0
to , the method adds a new line after each value and increments indent
the number of at the beginning of each line, that is, one indent
for equal to , and so on.1
\t
If indent
the argument is None
, then separator
the argument is equal to (', ', ': ')
, otherwise it is equal to (', ', ': ')
.
The following sample code demonstrates how to use json.dump()
the method to save data as a JSON file in Python.
with open("file.csv", "r") as file_csv:
fieldnames = ("field1", "field2")
reader = csv.DictReader(file_csv, fieldnames)
with open("myfile.json", "w") as file_json:
for row in reader:
json.dump(row, file_json)
Dataframe.to_json()
Convert CSV file to JSON file using method in Python
Pandas
The module's Dataframe.to_json(path, orient)
method takes DataFrame
and path
as input and converts them to a JSON string, saving it in the provided path
. If not provided path
, the method returns a JSON string as output, and if provided path
, the method returns nothing.
orient
The args parameter is useful for specifying how we want to format the JSON string, and has various options for both Series
the and inputs.DataFrame
Since Dataframe.to_json()
the method takes DataFrame
as input, we will pandas.readcsv()
first read the CSV file into using the method DataFrame
. The following sample code demonstrates how to Dataframe.to_json()
convert a CSV file into a JSON file in Python using the method.
import pandas as pd
csv_data = pd.read_csv("test.csv", sep=",")
csv_data.to_json("test.json", orient="records")
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