Loading [MathJax]/extensions/tex2jax.js

2019-02-25

Python: pandasでCSVファイルを読み込む際の文字コードによる使い分け

CSV形式のファイルをpandasを利用して読み込む場合、Shift-JISの場合にうまくいかなかったのでそのメモ書きです。 UTF-8の場合はread_csvファイルを使えばいいですが、Shift-JISの場合は、codecsでopenしてからread_tableでdataFrameに変換しましょう。

import codecs
import pandas as pd
recordFile=""
#UTF-8のCSVファイルを読む場合
alldata = pd.read_csv(recordFile)
#Shift-JISのCSVファイルを読む場合
with codecs.open(recordFile, "r", "Shift-JIS", "ignore") as file:
alldata = pd.read_table(file, delimiter=",")
view raw csvRead.py hosted with ❤ by GitHub