Problems when creating a csv-json conversion tool with python

When converting a csv file containing Japanese to a json file, I had a problem with character code conversion.

UnicodeDecodeError: 'ascii' codec can't decode byte...

I solved it with the following code assuming that the character code of the csv file to be read has been converted to utf-8n.

csvToJson.py


# -*- coding: utf-8 -*-
#module
import codecs
import csv
import json

#Open CSV file
f = open('Specify csv file to read', 'r')
reader = csv.DictReader(f, fieldnames = ("Field name 1", "Field name 2", ...,"Field name N"))
print "CSV loaded."

#Convert to JSON format
out = json.dumps( [row for row in reader], sort_keys=False, ensure_ascii=False, encoding='utf_8' )
print "JSON parserd."

#Save to JSON file
f=codecs.open('Specify json file to export','w', 'utf_8')
f.write(out)
print "JSON saved."

Consideration

There are many things that python beginners do not understand, but do you prevent garbled characters with the following internal processing? When reading a file with open, do not specify the character code, and use csv.DictReader to read the unicode object Create a dictionary. By setting ensure_ascii = False in json.dumps, unicode object can be handled, and by specifying utf-8 in encoding, the content of out object is set to utf-8, and Japanese is output without garbled characters. Are you able to do it?

Recommended Posts

Problems when creating a csv-json conversion tool with python
A memo when creating a python environment with miniconda
[Python] Creating a scraping tool Memo
Precautions when creating a Python generator
Creating a simple PowerPoint file with Python
Note when creating an environment with python
Precautions when solving DP problems with Python
Error when installing a module with Python pip
[Python3] A story stuck with time zone conversion
Procedure for creating a LineBot made with Python
Commands for creating a python3 environment with virtualenv
Creating a scraping tool
[Grasshopper] When creating a data tree on Python script
Try to make a command standby tool with python
Make a CSV formatting tool with Python Pandas PyInstaller
Current directory when creating a new one with Jupyter
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Creating an egg with python
Make a fortune with Python
Error when playing with python
Create a directory with python
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
A memo when face is detected with Python + OpenCV quickly
[python] A note when trying to use numpy with Cython
Use a macro that runs when saving python with vscode
A memo when creating a directed graph using Graphviz in Python
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
[Python] What a programming inexperienced person did before creating a tool
Create a simple video analysis tool with python wxpython + openCV
Creating a GUI as easily as possible with python [tkinter edition]
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
[5th] I tried to make a certain authenticator-like tool with python
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Precautions when creating a two-dimensional array with all the same values
[2nd] I tried to make a certain authenticator-like tool with python
Creating a decision tree with scikit-learn
Let's make a GUI with python.
Creating a Flask server with Docker
Solve ABC166 A ~ D with Python
[3rd] I tried to make a certain authenticator-like tool with python
Create a virtual environment with Python!
I made a fortune with Python.
MP3 to WAV conversion with Python
Building a virtual environment with Python 3
Solve AtCoder Problems Recommendation with python (20200517-0523)
Creating a simple app with flask
Solve ABC168 A ~ C with Python
Make a recommender system with python
A note I was addicted to when creating a table with SQLAlchemy
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
[4th] I tried to make a certain authenticator-like tool with python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!