2
1

More than 1 year has passed since last update.

はじめての記事投稿

.xlsx形式のスプレッドシートをPythonで使う

Posted at

csv形式のスプレッドシートの読み込み

csv形式のスプレッドシートを読み込む際は以下のようなコードで実行できます。

readcsv.py
with open('PATH/example.csv') as f:
    reader = csv.reader(f)

xlsx形式のスプレッドシートの読み込み

では、xlsx形式のスプレッドシートはどのようにして読み込むのでしょうか。
コードは以下の通り。

readxlsx.py
import pandas as pd
from openpyxl import Workbook

df = pd.read_excel('PATH/example.xlsx',engine='openpyxl', sheet_name='シート名', header=None)

また、例えば一列目の要素を配列に入れたい場合は以下のコードで実行可能。

readxlsx.py
url_list = df.iloc[:, 0].tolist()

出典

1. note.nkmk.me PythonでCSVファイルを読み込み・書き込み(入力・出力)
https://note.nkmk.me/python-csv-reader-writer/

2. Python Pandasを使ってExcel読み込みと書き込み
https://reffect.co.jp/python/python-pandas-excel/#excel-%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%81%BF

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1