|
Download i18n/i18n-0002.jsonl from winapiadmin/tinycodinglm: direct link, hf CLI and curl.
- Browser
- Download file 1.94 kB
-
https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/i18n/i18n-0002.jsonl
- Command line
-
hf download hf://datasets/winapiadmin/tinycodinglm/i18n/i18n-0002.jsonl
-
curl -L -o i18n-0002.jsonl https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/i18n/i18n-0002.jsonl
1.94 kB
| {"type": "session", "harness": "opencode", "id": "i18n-0002", "name": "Vietnamese UTF-8 file IO"} | |
| {"type": "message", "message": {"role": "system", "content": "You are a helpful software engineer. Answer in the same language the user writes in, and give correct, minimal Python examples."}} | |
| {"type": "message", "message": {"role": "user", "content": "Làm sao để đọc và ghi file văn bản tiếng Việt (UTF-8) trong Python mà không bị lỗi font hay mojibake?"}} | |
| {"type": "message", "message": {"role": "assistant", "content": "Nguyên nhân phổ biến nhất: khi mở file mà không ghi rõ `encoding`, Python dùng encoding mặc định của hệ điều hành (trên Windows thường là `cp1258` hoặc `cp1252`), nên các byte UTF-8 bị giải mã sai thành mojibake.\n\nCách làm đúng:\n1. Luôn ghi rõ `encoding=\"utf-8\"` khi đọc và ghi file.\n2. Khi đọc file không rõ nguồn gốc, dùng `encoding=\"utf-8-sig\"` — tự bỏ BOM nếu có, đọc bình thường nếu không có.\n3. Chuẩn hóa về NFC bằng `unicodedata.normalize` trước khi so sánh chuỗi.\n4. Đừng nhìn console cũ rồi kết luận file sai — console Windows cũ có thể không hiển thị được Unicode dù file vẫn đúng; hãy kiểm tra bằng byte hoặc editor hỗ trợ UTF-8.\n\n```python\nimport unicodedata\n\nTEXT = \"Tiếng Việt: xin chào thế giới, ễ ộ ữ ă â đ.\"\n\n# Ghi file luôn chỉ rõ encoding\nwith open(\"tieng_viet.txt\", \"w\", encoding=\"utf-8\") as f:\n f.write(TEXT)\n\n# Đọc file, tự xử lý BOM nếu có\nwith open(\"tieng_viet.txt\", \"r\", encoding=\"utf-8-sig\") as f:\n data = f.read()\n\n# Chuẩn hóa về NFC để so sánh chuỗi ổn định\ndata = unicodedata.normalize(\"NFC\", data)\nassert data == unicodedata.normalize(\"NFC\", TEXT)\nprint(f\"OK: {len(data)} ký tự, {len(data.encode('utf-8'))} bytes\")\n```"}} | |