Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: json2csv.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a JSON file (.json) to a CSV file (.csv).
- It reads the JSON file and writes the specified 'text_content' to the CSV file.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'json2csv.py'.
- 2. Ensure your JSON file ('example.json') is in the same directory as the script.
- 3. Run the script.
- 4. The converted CSV file ('json2csv.csv') will be generated in the same directory.
- Note: Adjust the 'json_filename' and 'csv_filename' variables in the script as needed.
- """
- import csv
- import json
- def json_to_csv(json_filename, csv_filename):
- with open(json_filename, 'r') as jsonfile, open(csv_filename, 'w', newline='') as csvfile:
- data = json.load(jsonfile)
- if "text_content" in data:
- csvwriter = csv.writer(csvfile)
- csvwriter.writerow([data["text_content"]])
- if __name__ == "__main__":
- json_filename = 'example.json'
- csv_filename = 'json2csv.csv'
- json_to_csv(json_filename, csv_filename)
- print(f"Converted '{json_filename}' to '{csv_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement