0
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 3 years have passed since last update.

【Python】CSVの最終行が指定データと一致するかどうか判断する関数

Last updated at Posted at 2021-09-07

車輪の再発明している感じ、ありますね。

def is_same_as_last_line(data, csv_file) -> bool:
    with open(csv_file, 'r', encoding='utf8') as f:
        rows = [row for row in csv.reader(f)]
    try:
        last_line = rows[-1]
        # 空白行が最後にある可能性があるので、その対策
        if last_line == []:
            last_line = rows[-2]
        if(last_line == data):
            return True
        return False
    # 行が少なすぎたりするとエラーを返す
    except IndexError:
        return False
0
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
0
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?