LoginSignup
0
3

More than 1 year has passed since last update.

PythonでCSVの読み書き

Last updated at Posted at 2022-04-14

listをcsvに書き込み,読み込みまたlistに戻す方法です.

import pandas as pd

pandasを使います.(他にも方法はありますが私の好みです)
初期装備ではないのでインストールが必要です.

CSVの書き込み

書き込みたいlistなどの変数をDataFrameに変換して保存できます.

df = pd.DataFrame(sample_lists)
df.to_csv('sample_lists.csv')

CSVの読み出し

df = pd.read_csv('sample_lists.csv', index_col=0)
sample_lists = df.values.tolist()

index_colをつけないと,自動でつけてくれた0.1.2.,,,のインデックスも一緒についてきてしまい邪魔です.

0
3
3

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
0
3