GrepBoard/Manuals
605 manuals ·

Turn a pasted table into CSV with Claude and check column counts

·Tested on Claude Sonnet (--model sonnet) · Claude Code 2.1.302 · Ubuntu 24.04 · 2026-10·Test scope ↓·7 steps

Web-copied tables have merged cells, commas in values and mixed units. Get CSV from a strict prompt, check each row by script, re-ask only the rows that fail.

A pasted table arrives tab-separated, with merged cells as blanks and commas and units inside the values. Pin the conversion rules in the prompt, then read the result with Python's csv module and check the column count and numeric fields row by row. For checking an answer against its source text, see summary with quotes.

[1] Put the table in a file. It is a made-up parts list with four traps on purpose: the category cell is blank where it merges with the row above, item names and prices contain commas (육각볼트 M6, 20mm · 1,200), weights mix g and kg, and some price, weight and stock cells are empty or not numbers (다음 입고 후 확정, "confirmed after next delivery"). Cells are separated by tabs.

table.txt
분류	품목	단가(원)	무게	재고볼트	육각볼트 M6, 20mm	1,200	35 g	340	육각볼트 M8, 30mm	1,850	0.06 kg		접시머리 볼트 M4	다음 입고 후 확정	18g	2,100너트	플랜지 너트 M6	900	12g	1,050	나비너트, M6		9 g	   0와셔	평와셔 M6 (100개입)	4,500	0.45 kg	75	스프링 와셔 M8 (100개입)	5,200		60	톱니 와셔 M6, 내치형	6,000	0.5kg	1,200

[2] Write the prompt. Wrap the table in <table> tags to keep it apart from the instructions. Five rules: fixed header, fill blank categories, quote fields with commas, integer format for number columns, never invent a value.

head.txt
Convert the table inside <table> to CSV (RFC 4180). Rules:- Header line, exactly: category,item,price_krw,weight_g,stock- One output line per data row. The input is tab-separated; a blank first cell means the row belongs to the category above it, so repeat that category.- Wrap a field in double quotes if it contains a comma. No quotes otherwise.- price_krw, weight_g and stock are plain integers: no thousands separators, no units. Convert kg to g. If a cell is empty or is not a number, leave the field empty (two commas in a row).- Never guess or fill in a value that is not in the table.- Output only the CSV, no code fence, no other text. <table>

[3] Append the table and the closing tag, then run. --tools "" turns tools off and --max-turns 1 ends after one answer. sonnet is an alias, so the model it points to can change over time.

console
$ (cat head.txt table.txt; echo "</table>") > prompt.txt$ claude -p --model sonnet --max-turns 1 --tools "" < prompt.txt > out.1.csv$ cat out.1.csvcategory,item,price_krw,weight_g,stock볼트,"육각볼트 M6, 20mm",1200,35,340볼트,"육각볼트 M8, 30mm",1850,60,볼트,접시머리 볼트 M4,,18,2100너트,플랜지 너트 M6,900,12,1050너트,"나비너트, M6",,9,0와셔,평와셔 M6 (100개입),4500,450,75와셔,스프링 와셔 M8 (100개입),5200,,60와셔,"톱니 와셔 M6, 내치형",6000,500,1200

Blank categories are filled, 0.06 kg became 60 and 0.45 kg became 450, and 다음 입고 후 확정 became an empty field. Only item names containing a comma got quotes. It looks right by eye, but you cannot eyeball hundreds of rows.

[4] Write the check script. It reads the file with csv.reader and tests four things: header identical, five columns on every row, price, weight and stock digits-only or empty, data row count equal to the source. Any failure gives exit code 1. The second argument, 8, is the number of data rows in the source.

check.py
#!/usr/bin/env python3"""usage: check.py out.csv expected_rows  — 열 수와 숫자 칸 검증"""import csv, sysHEADER = ["category", "item", "price_krw", "weight_g", "stock"]rows = list(csv.reader(open(sys.argv[1], encoding="utf-8")))want = int(sys.argv[2])bad = 0if rows[0] != HEADER:    print("HEADER", rows[0]); bad += 1for n, r in enumerate(rows[1:], 2):    errs = []    if len(r) != 5:        errs.append(f"{len(r)} columns")    else:        for name, v in zip(HEADER[2:], r[2:]):            if v and not v.isdigit():                errs.append(f"{name}={v!r}")        if not r[0] or not r[1]:            errs.append("category/item empty")    print(("BAD " if errs else "OK  ") + f"line {n}: {r}" + (" <- " + ", ".join(errs) if errs else ""))    bad += bool(errs)if len(rows) - 1 != want:    print(f"rows: {len(rows)-1}, expected {want}"); bad += 1print(f"{len(rows)-1} data rows, {bad} problems")sys.exit(1 if bad else 0)

[5] Run it. The first result passes all 8 rows.

console
$ python3 -I check.py out.1.csv 8 | tail -1; echo "exit ${PIPESTATUS[0]}"8 data rows, 0 problemsexit 0

The failing output comes from out-bad.csv, where the quotes on line 3 were removed by hand (the model did not make this error). Without quotes 육각볼트 M8, 30mm splits into two fields and the row has 6 columns.

console
$ sed '3s/"//g' out.1.csv > out-bad.csv$ python3 -I check.py out-bad.csv 8 | grep -v '^OK'; echo "exit ${PIPESTATUS[0]}"BAD line 3: ['볼트', '육각볼트 M8', ' 30mm', '1850', '60', ''] <- 6 columns8 data rows, 1 problemsexit 1

[6] Re-ask only the failing row. Re-running the whole table may change rows that were fine, so cut that row out of the source (line 3 of table.txt) and send it alone. A blank category cell cannot see the row above, so state the category in the prompt.

head-row.txt
Convert the single row inside <row> to one CSV line (RFC 4180). It is tab-separated: category, item, price_krw, weight_g, stock. The first cell is blank because the row belongs to the category 볼트 — fill that in. Rules:- Wrap a field in double quotes if it contains a comma. No quotes otherwise.- price_krw, weight_g and stock are plain integers: no thousands separators, no units. Convert kg to g. If a cell is empty or is not a number, leave the field empty.- Never guess or fill in a value that is not in the row.- Output only the CSV line, no header, no code fence, no other text. <row>

[7] Replace line 3 of out-bad.csv with the returned line and check again.

console
$ sed -n '3p' table.txt > row3.txt$ (cat head-row.txt row3.txt; echo "</row>") > prompt-row.txt$ claude -p --model sonnet --max-turns 1 --tools "" < prompt-row.txt > out.3.csv$ cat out.3.csv볼트,"육각볼트 M8, 30mm",1850,60,$ (sed -n '1,2p' out-bad.csv; cat out.3.csv; sed -n '4,$p' out-bad.csv) > out-fixed.csv$ python3 -I check.py out-fixed.csv 8 | tail -1; echo "exit ${PIPESTATUS[0]}"8 data rows, 0 problemsexit 0

The repaired file was identical to out.1.csv under diff.

The script checks shape only. If the column count and number format are right but a value is wrong, such as 0.06 kg turned into 6, it will not notice. For tables that matter, compare a few random rows against the source by hand.

#claude#csv#prompt