CSV行フィルター
列値の条件でCSV行をフィルタリングします。
このツールについて
CSV Row Filterは、カラムの値に基づいてCSVファイルの行をフィルタリングするブラウザベースのツールです。データセット、顧客記録、またはその他の表形式のデータを扱う場合、このツールはコードを書いたり複雑なスプレッドシート数式を使ったりすることなく、条件に一致する行を素早く抽出するのに役立ちます。
このツールを使用するには、CSVデータを入力フィールドに貼り付け、カラムを選択し、演算子(等しい、含む、より大きいなど)を選択し、値を入力してフィルタ条件を定義します。AND/ORロジックで複数の条件を組み合わせて、正確なフィルターを作成できます。一致した行は出力に直ちに表示され、コピー、ダウンロード、または追加のフィルターで詳しく調べることができます。
CSV Row Filterはデータアナリスト、開発者、および定期的に表形式のデータセットを扱う人に特に便利です。一般的な形式を自動的に処理し、元のデータを保持し、ブラウザ内で完全に実行されるため、サーバーにアップロードする必要がなく、CSVファイルを高速かつ安全に探索および処理できます。
よくある質問
コード実装
import csv
import io
def filter_csv(csv_text: str, column: str, condition: str, value: str) -> str:
"""
Filter CSV rows by condition on a column.
condition: 'contains' | 'equals' | 'gt' | 'lt' | 'not_contains'
"""
reader = csv.DictReader(io.StringIO(csv_text))
if reader.fieldnames is None:
return ""
rows = []
for row in reader:
cell = row.get(column, "")
if condition == "contains" and value.lower() in cell.lower():
rows.append(row)
elif condition == "equals" and cell == value:
rows.append(row)
elif condition == "not_contains" and value.lower() not in cell.lower():
rows.append(row)
elif condition in ("gt", "lt"):
try:
if condition == "gt" and float(cell) > float(value):
rows.append(row)
elif condition == "lt" and float(cell) < float(value):
rows.append(row)
except ValueError:
pass
out = io.StringIO()
writer = csv.DictWriter(out, fieldnames=reader.fieldnames)
writer.writeheader()
writer.writerows(rows)
return out.getvalue()
csv_data = """name,age,city
Alice,30,New York
Bob,25,London
Carol,35,New York"""
print(filter_csv(csv_data, "city", "equals", "New York"))
print(filter_csv(csv_data, "age", "gt", "28"))
Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.