Read CSV file with python (Download & parse CSV file)

1 Target data

The Tokyo Metropolitan Government Building has released data on the number of people infected with COVID-19. I would like to process this CSV data.

Number of infected people announced by the Tokyo Metropolitan Government https://catalog.data.metro.tokyo.lg.jp/dataset/t000010d0000000068/resource/c2d997db-1450-43fa-8037-ebb11ec28d4c (CSV file) https://stopcovid19.metro.tokyo.lg.jp/data/130001_tokyo_covid19_patients.csv

2 For the time being

The official documentation shows how to read the CSV file. https://docs.python.org/ja/3/library/csv.html

With reference to this, create a program that reads the CSV file downloaded in 1.

python


import csv
with open('130001_tokyo_covid19_patients.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in reader:
        print('■'.join(row))

When I do this I get the following error message

UnicodeDecodeError: 'cp932' codec can't decode byte 0xef in position 0: illegal multibyte sequence

Certainly cp932 was a word with a meaning like S-JIS. So, when I checked the character code of the CSV file, it was UTF-8.

For the coping method, I referred to this article. Reading Python UTF-8 CSV file (UnicodeDecodeError compatible)

It is recommended to specify encoding = "utf_8" for open.

3 Completed

This is the completed code

python


import csv
with open('130001_tokyo_covid19_patients.csv', encoding="utf_8") as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in reader:
        print('■'.join(row))

4 Others

The "row" in 3 was a list class. From now on, I'd like to add up daily, It seems to be troublesome as it is. It seems easier to use pandan, so I think I'll remake it with pandas ...

Recommended Posts

Read CSV file with python (Download & parse CSV file)
Download csv file with python
Read Python csv file
How to read a CSV file with Python 2/3
[Python] Write to csv file with Python
Implemented file download with Python + Bottle
Output to csv file with Python
[Python] Read the csv file and display the figure with matplotlib
Let's read the RINEX file with Python ①
[Automation] Read mail (msg file) with Python
[Python] Read a csv file with a large data size using a generator
Read CSV file: pandas
Csv tinkering with python
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Read JSON with Python and output as CSV
[Python] How to read excel file with pandas
Read table data in PDF file with Python
Read line by line from a file with Python
Read and format a csv file mixed with comma tabs with Python pandas
Read CSV file with Python and convert it to DataFrame as it is
Read and write csv file
Download the file in Python
I tried to touch the CSV file with Python
Draw netCDF file with python
Read QR code from image file with Python (Mac)
How to convert JSON file to CSV file with Python Pandas
Read json file with Python, format it, and output json
Write to csv with Python
Read json data with python
How to read an Excel file (.xlsx) with Pandas [Python]
Extract the xz file with python
[Automation with python! ] Part 1: Setting file
Create an Excel file with Python3
[python] Read information with Redmine API
Download the file deployed with appcfg.py
Python CSV file reading and writing
Handle Excel CSV files with Python
Read files in parallel with Python
Reading and writing CSV with Python
[Automation with python! ] Part 2: File operation
Read fbx from python with cinema4d
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
Read a file in Python with a relative path from the program
Download python
Read file
[Python] How to convert db file to csv
Creating a simple PowerPoint file with Python
HTTP split download guy made with Python
[Python] Convert csv file delimiters to tab delimiters
Check the existence of the file with python
Quickly create an excel file with Python #python
Download Japanese stock price data with python
[C] [python] Read with AquesTalk on Linux
Read and write csv files with numpy
Read the file line by line in Python
Download files on the web with Python
Read the file line by line in Python
Read Python csv and export to txt
Create Excel file with Python + similarity matrix
[Python] Read images with OpenCV (for beginners)
Record with Python → Save file (sounddevice + wave)