python3

python3 で、csv ファイルを読み込んで条件をみたす行の内容を表示する方法

投稿日:

csv.reader で読み込み、各行を読み込んで判定する。

import csv

with open('data.csv', 'r') as csv_file:
    reader = csv.reader(csv_file)
    for row in reader:
        if row[2] == '売り':
            print(row[0] + ' ' + row[1] + ' ' + row[3])

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/03/08 12:20:32 USD 9876

-python3
-

執筆者:


comment

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

関連記事

no image

matplotlib で csv ファイルからデータを読み込んでグラフを表示する方法

np loadtext で csv ファイルを読み込む。 pyplot.bar で棒グラフを表示する。 例 import numpy as np from matplotlib import pypl …

no image

matplotlib で、線の種類を変更する方法

「linestyle = 」として、線の種類を指定することができる設定の詳細は、次のリンクを参照。 https://matplotlib.org/stable/gallery/lines_bars_a …

no image

gnuplot で、グラフの形を正方形にする方法

set size square または、その省略形で set size sq というコマンドを使えば正方形の中にグラフを描くことができる。 例 set size square plot cos(x) …

no image

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

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

no image

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

next() を使う。 例 my_arr = [‘春’,’夏’,’秋’,’冬’] my_iter = iter(my_arr) print(next(my_iter,’なし’)) print(next …