Remove Special Characters
remove_special_chars · text · text
2026-09-23
กำลังประมวลผล…
remove_special_chars · text · text
2026-09-23
ข้อมูลที่พิมพ์มือมักมีสัญลักษณ์ติดมา เช่น "ชื่อ: สมชาย *" หรือ "฿1,250.00" เทคนิคนี้ทำงานแบบ allowlist คือบอกว่าจะ เก็บ อะไรไว้ แล้วลบทุกอย่างที่เหลือ ซึ่งคาดเดาผลได้ง่ายกว่าการไล่ลบทีละสัญลักษณ์
strip_currency และ remove_thousand_separator ซึ่งแปลงเป็นตัวเลขจริงให้ด้วย@, ., +, / เป็นส่วนสำคัญของค่า ใช้ tool หมวด Validity แทนkeepLetters (ตัวอักษรทุกภาษารวมสระและวรรณยุกต์ไทย), keepDigits, keepSpaces และ keepChars ที่พิมพ์เพิ่มเองระวัง:
keepLettersเก็บ\p{M}ไว้ด้วย ถ้าไม่มีส่วนนี้ สระและวรรณยุกต์ไทยจะถูกลบจนคำอ่านไม่ออก
| Parameter | Type | Default |
|---|---|---|
columns | string[] | — |
keepLetters | boolean | true |
keepDigits | boolean | true |
keepSpaces | boolean | true |
keepChars | string | "" |
import json
import unicodedata
def keep_char(ch, keep_letters, keep_digits, keep_spaces, keep_chars):
category = unicodedata.category(ch)
if keep_letters and (category.startswith('L') or category.startswith('M')):
return True
if keep_digits and category.startswith('N'):
return True
if keep_spaces and ch.isspace():
return True
return ch in keep_chars
def remove_special_chars(value, keep_letters=True, keep_digits=True, keep_spaces=True, keep_chars=''):
if not isinstance(value, str):
return value
return ''.join(ch for ch in value if keep_char(ch, keep_letters, keep_digits, keep_spaces, keep_chars))
values = ['฿1,250.00', 'e-mail: a@x.com', 'ไทย!', None]
result = [remove_special_chars(value) for value in values]
print(json.dumps(result, ensure_ascii=False))
คอลัมน์ product_code มีค่าอย่าง "SKU-001 *" และ "SKU—001" (ขีดยาว) ตั้ง keepLetters และ keepDigits เป็นจริง ปิด keepSpaces แล้วใส่ - ใน keepChars เพื่อให้เหลือ SKU-001 เหมือนกันทุกแถว ตรวจ Preview ก่อนเสมอว่าไม่มีรหัสที่ต้องใช้ / หรือ . หายไปด้วย
ใช้ remove_line_breaks สำหรับการขึ้นบรรทัดใหม่โดยเฉพาะ ใช้ strip_currency กับคอลัมน์เงิน และใช้ validate_regex เพื่อตรวจว่าค่าที่เหลือตรงรูปแบบที่ต้องการจริง