python3

python3 で csv ファイルを読み込んで、最初の数行を表示する方法

投稿日:

以下の例では、空の配列 data を用意しておき、最初の3行を読み込んでおく。

import csv

with open('data.csv','r') as csv_file:
    csv_reader = csv.reader(csv_file)
    data = []
    for row in csv_reader:
        data.append(row)
    for row in data[:3]:
        print(row)

data.csv

"2021/01/10 02:34:10","JPY","買い","1,713.12"
"2022/03/08 12:20:32","USD","売り","9876"
"2022/12/31 18:07:55","EUR","買い","1,234,888.9"
"2022/12/31 18:07:55","EUR","買い","1,111.9"
"2022/12/31 20:08:55","EUR","買い","10"
"2023/03/31 18:07:55","USD","売り","34,888"
"2023/04/01 08:17:55","USD","売り","34,888"
"2023/05/10 09:24:05","USD","売り","34,888"

結果

['2021/01/10 02:34:10', 'JPY', '買い', '1,713.12']
['2022/03/08 12:20:32', 'USD', '売り', '9876']
['2022/12/31 18:07:55', 'EUR', '買い', '1,234,888.9']

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で複数のグラフを並べて表示する方法

plot.subplots() でグラフの縦方向と横方向の数を指定する。 例 import numpy as np from matplotlib import pyplot as plt x1 = …

no image

matplotlib で、y軸に二種類の軸を設定する方法

y軸に、2種類のグラフを異なるスケールでプロットする。 matplotlib axes の twinx() を使う。 https://matplotlib.org/stable/api/_as_gen …

no image

matplotlib で、グラフのタイトルに上付き文字を入力する方法

数学記号などで、グラフのタイトルに上付きの文字を入力したい場合がある。 その場合は次のように書いて設定できる。 例 import numpy as np from matplotlib import …

no image

python3 で、フォルダがなければ作成するプログラム

os.mkdir() 関数を使う。 すでにフォルダが存在している場合は、エラーを表示する。 例 import os folder_name = ‘./python_create_folder’ try …

no image

matplotlib で、ヒストグラムを作成する方法

ヒストグラムを描くには、data_hist を使う。 ヒストグラムの縦棒の数は bins で指定する。 例 from matplotlib import pyplot as plt data_hist …