2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonにCSVなどのサンプルデータをヒアドキュメントとして埋め込む際にはio.StringIOが便利

Last updated at Posted at 2022-08-01

小ネタです。

類似のサンプルはよく見ると見つかるのですが、キーワード検索で引っかかりやすくるために記事を立てました。

Pythonのライブラリ、Pandasの例を記載する場合に、テーブルのサンプルデータをコード中に(ヒアドキュメントとして)埋め込見たくなる場合があります。こういった場合には io.StringIO を活用して以下のような書き方をすると比較的可読性と両立しやすいように思います。

import io
import pandas pd
data = '''this,is,header
01,12,sample
'''
df = pd.read_csv(io.StringIO(data))
2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?