Spaces:
Sleeping
Sleeping
feat: option "garder uniquement les chiffres" (retire lettres/symboles des montants)
Nouvelle option opt-in keep_digits_only : sur les cellules contenant un nombre,
retire lettres, symboles et espaces en gardant chiffres et separateurs decimaux
(12,50EUR -> 12,50, A123 -> 123, 10 ml -> 10). Les cellules sans chiffre sont
laissees intactes. Corrige les imports EBP qui cassaient sur des montants pollues.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@
- app.py +2 -0
- core/cleaner.py +30 -0
- static/index.html +9 -1
- tests/test_cleaner.py +22 -1
app.py
CHANGED
|
@@ -53,6 +53,7 @@ async def clean(
|
|
| 53 |
drop_duplicates: bool = Form(True),
|
| 54 |
trim_whitespace: bool = Form(True),
|
| 55 |
normalize_headers: bool = Form(True),
|
|
|
|
| 56 |
decimal_comma: bool = Form(False),
|
| 57 |
ebp_format: bool = Form(False),
|
| 58 |
):
|
|
@@ -81,6 +82,7 @@ async def clean(
|
|
| 81 |
"drop_duplicates": drop_duplicates,
|
| 82 |
"trim_whitespace": trim_whitespace,
|
| 83 |
"normalize_headers": normalize_headers,
|
|
|
|
| 84 |
"decimal_comma": decimal_comma,
|
| 85 |
"ebp_format": ebp_format,
|
| 86 |
}
|
|
|
|
| 53 |
drop_duplicates: bool = Form(True),
|
| 54 |
trim_whitespace: bool = Form(True),
|
| 55 |
normalize_headers: bool = Form(True),
|
| 56 |
+
keep_digits_only: bool = Form(False),
|
| 57 |
decimal_comma: bool = Form(False),
|
| 58 |
ebp_format: bool = Form(False),
|
| 59 |
):
|
|
|
|
| 82 |
"drop_duplicates": drop_duplicates,
|
| 83 |
"trim_whitespace": trim_whitespace,
|
| 84 |
"normalize_headers": normalize_headers,
|
| 85 |
+
"keep_digits_only": keep_digits_only,
|
| 86 |
"decimal_comma": decimal_comma,
|
| 87 |
"ebp_format": ebp_format,
|
| 88 |
}
|
core/cleaner.py
CHANGED
|
@@ -82,6 +82,19 @@ def _looks_numeric(value: str) -> bool:
|
|
| 82 |
return bool(_NUM_RE.match(value)) and any(c.isdigit() for c in value)
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def _to_decimal_comma(value: str) -> str:
|
| 86 |
"""Normalise un nombre vers la virgule décimale française, sans séparateur de milliers."""
|
| 87 |
v = value.strip().replace(" ", " ")
|
|
@@ -186,6 +199,23 @@ def clean_csv(raw: bytes, options: dict) -> tuple[bytes, dict]:
|
|
| 186 |
if removed:
|
| 187 |
log(f"{removed} ligne(s) en double supprimée(s).")
|
| 188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
# 5. Décimales en virgule française
|
| 190 |
if options.get("decimal_comma"):
|
| 191 |
converted = 0
|
|
|
|
| 82 |
return bool(_NUM_RE.match(value)) and any(c.isdigit() for c in value)
|
| 83 |
|
| 84 |
|
| 85 |
+
def _strip_to_digits(value: str) -> str:
|
| 86 |
+
"""
|
| 87 |
+
Retire lettres, symboles et espaces, ne garde que les chiffres et les
|
| 88 |
+
séparateurs décimaux (, . -). Ex. : "12,50€" → "12,50", "A123" → "123",
|
| 89 |
+
"10 ml" → "10". Les cellules sans aucun chiffre sont laissées intactes.
|
| 90 |
+
"""
|
| 91 |
+
if not any(c.isdigit() for c in value):
|
| 92 |
+
return value
|
| 93 |
+
core = re.sub(r"[^\d.,\-]", "", value)
|
| 94 |
+
core = core.strip(".,") # retire d'éventuels séparateurs en bord
|
| 95 |
+
return core if any(c.isdigit() for c in core) else value
|
| 96 |
+
|
| 97 |
+
|
| 98 |
def _to_decimal_comma(value: str) -> str:
|
| 99 |
"""Normalise un nombre vers la virgule décimale française, sans séparateur de milliers."""
|
| 100 |
v = value.strip().replace(" ", " ")
|
|
|
|
| 199 |
if removed:
|
| 200 |
log(f"{removed} ligne(s) en double supprimée(s).")
|
| 201 |
|
| 202 |
+
# 4b. Garder uniquement les chiffres (retire lettres/symboles des montants)
|
| 203 |
+
if options.get("keep_digits_only"):
|
| 204 |
+
stripped = 0
|
| 205 |
+
|
| 206 |
+
def strip_cell(x):
|
| 207 |
+
nonlocal stripped
|
| 208 |
+
if isinstance(x, str):
|
| 209 |
+
new = _strip_to_digits(x)
|
| 210 |
+
if new != x:
|
| 211 |
+
stripped += 1
|
| 212 |
+
return new
|
| 213 |
+
return x
|
| 214 |
+
|
| 215 |
+
df = df.apply(lambda col: col.map(strip_cell))
|
| 216 |
+
if stripped:
|
| 217 |
+
log(f"{stripped} cellule(s) nettoyée(s) : lettres et symboles retirés.")
|
| 218 |
+
|
| 219 |
# 5. Décimales en virgule française
|
| 220 |
if options.get("decimal_comma"):
|
| 221 |
converted = 0
|
static/index.html
CHANGED
|
@@ -177,6 +177,14 @@
|
|
| 177 |
</span>
|
| 178 |
</label>
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
<label class="opt">
|
| 181 |
<input type="checkbox" name="decimal_comma" />
|
| 182 |
<span class="opt-text">
|
|
@@ -260,7 +268,7 @@
|
|
| 260 |
const base = window.location.origin;
|
| 261 |
const fd = new FormData();
|
| 262 |
fd.append('csv_file', csvInput.files[0]);
|
| 263 |
-
['drop_empty','drop_duplicates','trim_whitespace','normalize_headers','decimal_comma','ebp_format']
|
| 264 |
.forEach(name => {
|
| 265 |
const el = form.querySelector(`[name="${name}"]`);
|
| 266 |
fd.append(name, el.checked ? 'true' : 'false');
|
|
|
|
| 177 |
</span>
|
| 178 |
</label>
|
| 179 |
|
| 180 |
+
<label class="opt">
|
| 181 |
+
<input type="checkbox" name="keep_digits_only" />
|
| 182 |
+
<span class="opt-text">
|
| 183 |
+
<span class="opt-title">Garder uniquement les chiffres</span>
|
| 184 |
+
<span class="opt-desc">Retire lettres et symboles des cellules contenant un nombre (12,50€ → 12,50, A123 → 123). N'active que si une colonne de montants contient des lettres.</span>
|
| 185 |
+
</span>
|
| 186 |
+
</label>
|
| 187 |
+
|
| 188 |
<label class="opt">
|
| 189 |
<input type="checkbox" name="decimal_comma" />
|
| 190 |
<span class="opt-text">
|
|
|
|
| 268 |
const base = window.location.origin;
|
| 269 |
const fd = new FormData();
|
| 270 |
fd.append('csv_file', csvInput.files[0]);
|
| 271 |
+
['drop_empty','drop_duplicates','trim_whitespace','normalize_headers','keep_digits_only','decimal_comma','ebp_format']
|
| 272 |
.forEach(name => {
|
| 273 |
const el = form.querySelector(`[name="${name}"]`);
|
| 274 |
fd.append(name, el.checked ? 'true' : 'false');
|
tests/test_cleaner.py
CHANGED
|
@@ -4,7 +4,8 @@ from core.cleaner import clean_csv, detect_delimiter, detect_encoding
|
|
| 4 |
|
| 5 |
def _opts(**over):
|
| 6 |
base = dict(drop_empty=False, drop_duplicates=False, trim_whitespace=False,
|
| 7 |
-
normalize_headers=False,
|
|
|
|
| 8 |
base.update(over)
|
| 9 |
return base
|
| 10 |
|
|
@@ -75,6 +76,26 @@ def test_ebp_format_output():
|
|
| 75 |
assert b"\r\n" in out
|
| 76 |
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def test_no_options_reports_clean():
|
| 79 |
raw = b"a,b\n1,2\n"
|
| 80 |
_out, rep = clean_csv(raw, _opts())
|
|
|
|
| 4 |
|
| 5 |
def _opts(**over):
|
| 6 |
base = dict(drop_empty=False, drop_duplicates=False, trim_whitespace=False,
|
| 7 |
+
normalize_headers=False, keep_digits_only=False,
|
| 8 |
+
decimal_comma=False, ebp_format=False)
|
| 9 |
base.update(over)
|
| 10 |
return base
|
| 11 |
|
|
|
|
| 76 |
assert b"\r\n" in out
|
| 77 |
|
| 78 |
|
| 79 |
+
def test_keep_digits_only_strips_letters_symbols():
|
| 80 |
+
raw = "p\n12,50€\nA123\n10 ml\n€99.90\n".encode("utf-8")
|
| 81 |
+
out, _ = clean_csv(raw, _opts(keep_digits_only=True))
|
| 82 |
+
body = out.decode("utf-8-sig").splitlines()[1:]
|
| 83 |
+
assert body == ["12,50", "123", "10", "99.90"]
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def test_keep_digits_only_leaves_pure_text():
|
| 87 |
+
raw = "nom\nCHOREGRAPHIE\nVELOURS\n".encode("utf-8")
|
| 88 |
+
out, _ = clean_csv(raw, _opts(keep_digits_only=True))
|
| 89 |
+
body = out.decode("utf-8-sig").splitlines()[1:]
|
| 90 |
+
assert body == ["CHOREGRAPHIE", "VELOURS"]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def test_keep_digits_then_decimal_comma():
|
| 94 |
+
raw = "prix\n12.50€\n".encode("utf-8")
|
| 95 |
+
out, _ = clean_csv(raw, _opts(keep_digits_only=True, decimal_comma=True))
|
| 96 |
+
assert out.decode("utf-8-sig").splitlines()[1] == "12,50"
|
| 97 |
+
|
| 98 |
+
|
| 99 |
def test_no_options_reports_clean():
|
| 100 |
raw = b"a,b\n1,2\n"
|
| 101 |
_out, rep = clean_csv(raw, _opts())
|