Python script to create a JSON file from a CSV file

It is easy to create a CSV file using a spreadsheet (such as Excel) manually to create a lot of data with the same item. However, when I pass it to another tool, I sometimes have to use a JSON file, so I made a Python script to use in that case. I needed JSON-formatted data to run Python unittests in a data-driven manner.

Python unittest DDT > Example usage

Premise of use

OS : Windows Python : v3.5.1

spec

Source

The JSON file is indented so that it has a layout that is easy for people to read. When I put a Japanese comment, I didn't put it because an encoding error occurred in the json.dumps line at runtime. (I couldn't solve it myself, so I put it on hold. I wonder if all of Linux and Mac will not give an error in UTF-8.)

script.py


# -*- coding: utf-8 -*-
import csv
import json
import sys, os, codecs

argvs = sys.argv
for csv_file_path in argvs:
    if csv_file_path == __file__:
        continue
    with codecs.open(csv_file_path, 'r', 'shift_jis') as f:
        json_reader = csv.reader(f)
        name, ext = os.path.splitext(os.path.basename(csv_file_path))
        with codecs.open(name + '_datapool.json', 'w', 'shift_jis') as json_file:
            out = json.dumps([line_json for i, line_json in enumerate(json_reader) if i > 0] ,ensure_ascii=False, indent=4, sort_keys=False, separators=(',', ': '))
            json_file.write(out)

How to use

Execute the above script file with the csv file as an argument. (Multiple specifications allowed)

>python script.py csv_file1.csv

The JSON file example that is created is as follows.

csv_file1_datapool.json


[
    [
        "Test 1",
        "Test 2"
    ],
    [
        "Tesuto 1",
        "Street fighter 2"
    ]
]

reference

Recommended Posts

Python script to create a JSON file from a CSV file
How to create a JSON file in Python
Script to generate directory from json file
Create a deb file from a python package
Script to create a Mac dictionary file
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
Create a shell script to run the python file multiple times
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
Create a New Todoist Task from Python Script
Parse a JSON string written to a file in Python
How to convert JSON file to CSV file with Python Pandas
How to create a kubernetes pod from python code
Aggregate steps by day from iPhone healthcare data to create a CSV file
Execute Python script from batch file
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Output to csv file with Python
How to create a config file
A Python script that saves a clipboard (GTK) image to a file.
How to run a Python program from within a shell script
A python script that converts Oracle Database data to csv
Script python file
[Python] How to convert db file to csv
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
I made a script in Python to convert a text file for JSON (for vscode user snippet)
Send a message from Python to Slack
Create a JSON object mapper in Python
[Python] How to store a csv file as one-dimensional array data
Python vba to create a date string for creating a file name
[GPS] Create a kml file in Python
How to create a CSV dummy file containing Japanese using Faker
Python> Output numbers from 1 to 100, 501 to 600> For csv
How to create a repository from media
Use Django from a local Python script
How to run a Maya Python script
Create a shortcut to run a Python file in VScode on your terminal
Create a tool to automatically furigana with html using Mecab from Python3
[Python] How to read a csv file (read_csv method of pandas module)
Python script that outputs all records of Oracle table to CSV file
Send a message from Slack to a Python server
[Python] List Comprehension Various ways to create a list
Run a Python script from a C # GUI application
Create a GIF file using Pillow in Python
How to create a Python virtual environment (venv)
How to open a web browser from python
How to create a function object from a string
Create a C array from a Python> Excel sheet
[Python] Convert from DICOM to PNG or CSV
Import Excel file from Python (register to DB)
I want to create a window in Python
From file to graph drawing in Python. Elementary elementary
Script to create FlatBuffers binaries from SQL database
Writing logs to CSV file (Python, C language)
[python] Create table from pandas DataFrame to postgres
I tried reading a CSV file using Python
"Python Kit" that calls a Python script from Swift
Steps to create a Twitter bot with python
Export Python3 version OpenCV KeyPoint to a file
Draw a graph with matplotlib from a csv file
Create a decision tree from 0 with Python (1. Overview)