kopia lustrzana https://github.com/animator/learn-python
json-to-csv advance python
rodzic
406004d9c9
commit
4c6318e083
|
@ -1,3 +1,4 @@
|
|||
# List of sections
|
||||
|
||||
- [Decorators/\*args/**kwargs](decorator-kwargs-args.md)
|
||||
- [JSON to CSV](json-to-csv.md)
|
|
@ -0,0 +1,98 @@
|
|||
# JSON File into a CSV
|
||||
This file explains a script which takes a JSON file as input and generates a CSV file as output. It utilizes the `json module` for handling JSON data and the `csv module` for writing the CSV file.
|
||||
|
||||
# Modules
|
||||
## 1. JSON
|
||||
The json module in Python is used for parsing `JSON (JavaScript Object Notation)` data. JSON is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. The json module provides methods to convert between JSON strings and Python objects.
|
||||
|
||||
### Key functions in the json module include:
|
||||
|
||||
1. `json.load()`: This function parses a JSON file and converts it into a Python dictionary or list.
|
||||
|
||||
2. `json.dump()`: This function serializes a Python dictionary or list into a JSON formatted string and writes it to a file.
|
||||
|
||||
To install the json module, you can use the following pip command: `pip install json`
|
||||
|
||||
However, the json module is part of the Python Standard Library, so it is available by default and does not need to be installed separately.
|
||||
|
||||
## 2. CSV
|
||||
The csv module is part of the Python Standard Library and is used to read from and write to `CSV (Comma Separated Values)` files. CSV is a common format for data interchange that is supported by many applications.
|
||||
|
||||
### Key functions in the csv module include:
|
||||
|
||||
1. `csv.reader()`: This function reads data from a CSV file.
|
||||
2. `csv.writer()`: This function writes data to a CSV file.
|
||||
|
||||
# Code Explanation
|
||||
The following code demonstrates how to convert a JSON file into a CSV file. It reads the JSON data from the input file, processes it, and writes it to the output CSV file.
|
||||
|
||||
```python
|
||||
import json
|
||||
import csv
|
||||
|
||||
def json_to_csv(json_file_path, csv_file_path):
|
||||
# Open the JSON file and load the data
|
||||
with open(json_file_path, 'r') as json_file:
|
||||
data = json.load(json_file)
|
||||
|
||||
# Open the CSV file for writing
|
||||
with open(csv_file_path, 'w', newline='') as csv_file:
|
||||
# Create a CSV writer object
|
||||
csv_writer = csv.writer(csv_file)
|
||||
|
||||
# Write the header row
|
||||
header = data[0].keys()
|
||||
csv_writer.writerow(header)
|
||||
|
||||
# Write the data rows
|
||||
for row in data:
|
||||
csv_writer.writerow(row.values())
|
||||
```
|
||||
## Example usage
|
||||
json_file_path = `'input.json'`</br>
|
||||
csv_file_path = `'output.csv'`</br>
|
||||
<hr>
|
||||
|
||||
`input.json`
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"Name": "Yatharth",
|
||||
"age": 21,
|
||||
"birthyear": "2003"
|
||||
},
|
||||
{
|
||||
"Name": "Sangita",
|
||||
"age": 53,
|
||||
"birthyear": "1971"
|
||||
}
|
||||
]
|
||||
```
|
||||
<hr>
|
||||
|
||||
`output.csv`
|
||||
|
||||
|
||||
| Name | Age | Birthyear |
|
||||
|----------|-----|-----------|
|
||||
| Yatharth | 21 | 2003 |
|
||||
| Sangita | 53 | 1971 |
|
||||
|
||||
|
||||
|
||||
## Detailed Steps
|
||||
1. <b>Import Modules</b>: The script starts by importing the json and csv modules.
|
||||
|
||||
2. <b>Define the Function</b>: The json_to_csv function is defined to take two arguments: json_file_path (the path to the input JSON file) and csv_file_path (the path to the output CSV file).
|
||||
|
||||
3. <b>Read JSON Data</b>: The JSON file is opened in read mode using a with statement to ensure it is properly closed after reading.
|
||||
The json.load function reads the JSON data and converts it into a Python list or dictionary.
|
||||
4. <b>Write CSV Data</b>: The CSV file is opened in write mode using a with statement.
|
||||
A CSV writer object is created using csv.writer.
|
||||
The header row is written to the CSV file. This assumes that the JSON data is a list of dictionaries, and the keys of the first dictionary are used as the header.
|
||||
The data rows are written to the CSV file. Each dictionary's values are written as a row in the CSV file.
|
||||
</br>
|
||||
<hr>
|
||||
|
||||
This script provides a straightforward way to convert JSON data into CSV format, making it easier to work with in various applications, such as spreadsheet software or data analysis tools. You can customize the behavior of the script by modifying the source code according to your requirements.
|
Ładowanie…
Reference in New Issue