Data input / output in Python (CSV, JSON)

Overview

I am studying with reference to O'Reilly Japan's "Data Visualization Beginning with Python and JavaScript".

module

Output JSON format file in CSV format

# coding:UTF-8
import json
import csv

#Load JSON file
json_dict = json.load(open('data/players.json', 'r'))
#Extraction of list of dict
target_dicts = json_dict['players']

with open('data/players.csv', 'w') as f:
    #Dialect registration
    csv.register_dialect('dialect01', doublequote=True, quoting=csv.QUOTE_ALL)
    #DictWriter creation
    writer = csv.DictWriter(f, fieldnames=target_dicts[0].keys(), dialect='dialect01')
    #Write to CSV
    writer.writeheader()
    for target_dict in target_dicts:
        writer.writerow(target_dict)

Output CSV format file in JSON format

# coding:UTF-8
import json
import csv

json_list = []
json_data = {}

#Load CSV file
with open('data/players.csv', 'r') as f:
    #Creating a list of dict
    for line in csv.DictReader(f):
        json_list.append(line)

    json_data["players"] = json_list

with open('data/players.json', 'w') as f:
    #Write to JSON
    json.dump(json_data, f)

Input / output files

JSON format (players.json)

{
  "players": [
    {
      "id": "0001",
      "name": "Nishikawa Haruki",
      "position": "center fielder"
    },
    {
      "id": "0002",
      "name": "Matsumoto Go",
      "position": "right fielder"
    },
    {
      "id": "0003",
      "name": "Brandon J. Laird",
      "position": "third baseman"
    } ,
    {
      "id": "0004",
      "name": "Nakata Sho",
      "position": "first baseman"
    }
  ]
}

CSV format (players.csv)

"id","name","position"
"0001","Nishikawa Haruki","center fielder"
"0002","Matsumoto Go","right fielder"
"0003","Brandon J. Laird","third baseman"
"0004","Nakata Sho","first baseman"

reference

Books

Data visualization starting with Python and JavaScript https://www.oreilly.co.jp/books/9784873118086/

Python 3.3.6 documentation

14.1. csv — Read and write CSV files https://docs.python.jp/3.3/library/csv.html

19.2. json — JSON encoder and decoder https://docs.python.jp/3.3/library/json.html

Recommended Posts

Data input / output in Python (CSV, JSON)
Csv in python
Key input in Python
Handling json in python
python input and output
Python audio input / output
Japanese output in Python
Key input in Python
Read JSON with Python and output as CSV
Reading and writing CSV and JSON files in Python
Speed evaluation of CSV file output in Python
CSV output of pulse data with Raspberry Pi (confirm analog input with python)
Handle Ambient data in Python
Easily format JSON in Python
Full-width and half-width processing of CSV data in Python
Display UTM-30LX data in Python
Collectively register data in Firestore using csv file in Python
[Python] How to name table data and output it in csv (to_csv method)
Python Input Note in AtCoder
Output log in JSON format with Python standard logging
Read Fortran output in python
Read json data with python
Output Excel data in separate writing using Python3 + xlrd + mecab
Python application: Data handling Part 1: Data formatting and file input / output
Transpose CSV files in Python Part 1
Output 2017 Premium Friday list in Python
Tips on Python file input / output
Read Protocol Buffers data in Python3
Write JSON Schema in Python DSL
Get data from Quandl in Python
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
Handle NetCDF format data in Python
Dynamically load json type in python
Handling of JSON files in Python
Make standard output non-blocking in Python
Hashing data in R and Python
Let's see using input in python
Notes for Python file input / output
Export and output files in Python
Python #JSON
Get additional data in LDAP with python
Data handling 1 Data formatting and file input / output
Ant book in python: Sec. 2-4, data structures
Output tree structure of files in Python
Python: Reading JSON data from web API
Try working with binary data in Python
Create a JSON object mapper in Python
Convert Excel data to JSON with python
Get Google Fit API data in Python
Python: Preprocessing in machine learning: Data acquisition
Get Youtube data in Python using Youtube Data API
I saved the scraped data in CSV!
Read and write JSON files in Python
Easily graph data in shell and Python
Python> Output numbers from 1 to 100, 501 to 600> For csv
Python: Preprocessing in machine learning: Data conversion
[Python] Chapter 02-03 Basics of Python programs (input / output)
Put OpenCV in OS X with Homebrew and input / output video with python
How to store CSV data in Amazon Kinesis Streams with standard input
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python