Files
TJWaterServerBinary/api_ex/remove_sb_columns.py
2025-10-30 00:39:29 +08:00

33 lines
1.1 KiB
Python

import csv
from pathlib import Path
# infile = Path(r"c:\copilot codes\dataclean\Flow_Timedata2025_new_format.csv")
# outfile = Path(r"c:\copilot codes\dataclean\szh_flow_scada.csv")
infile = Path(r"c:\copilot codes\dataclean\Pressure_Timedata2025_new_format.csv")
outfile = Path(r"c:\copilot codes\dataclean\szh_pressure_scada.csv")
with infile.open("r", newline="", encoding="utf-8") as f_in:
reader = csv.reader(f_in)
rows = list(reader)
if not rows:
print("input file is empty")
raise SystemExit(1)
headers = rows[0]
# keep columns whose header does NOT contain 'SB_'
keep_indices = [i for i,h in enumerate(headers) if 'SB_' not in h]
removed = [h for i,h in enumerate(headers) if 'SB_' in h]
with outfile.open("w", newline="", encoding="utf-8") as f_out:
writer = csv.writer(f_out)
for row in rows:
# ensure row has same length as headers
if len(row) < len(headers):
row = row + [''] * (len(headers) - len(row))
newrow = [row[i] for i in keep_indices]
writer.writerow(newrow)
print(f"Wrote {outfile} — removed {len(removed)} columns containing 'SB_'.")