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

python3 で、リストを pop したものの返り値

リストを pop すると、 pop された値が返される。 例 plant_1 = [’pumpkin’, ‘ginger’, ‘potato’] plant_2 = [] print( …

no image

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

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

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

リストを iter に変えたあと、… next() を使う。 参考リンク https://www.programiz.com/python-programming/methods/buil …

no image

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

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