|
Download i18n/i18n-0004.jsonl from winapiadmin/tinycodinglm: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/i18n/i18n-0004.jsonl
- Command line
-
hf download hf://datasets/winapiadmin/tinycodinglm/i18n/i18n-0004.jsonl
-
curl -L -o i18n-0004.jsonl https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/i18n/i18n-0004.jsonl
1.8 kB
| {"type": "session", "harness": "opencode", "id": "i18n-0004", "name": "Japanese text handling in Python"} | |
| {"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": "Python で日本語(ひらがな・カタカナ・漢字)を読み書きするときの注意点を教えてください。コード例もお願いします。"}} | |
| {"type": "message", "message": {"role": "assistant", "content": "一番多い原因:`open()` で `encoding` を指定しないと OS の既定エンコーディング(Windows では cp932)が使われ、UTF-8 のバイト列が誤って解釈されて文字化けします。\n\n正しい方法:\n1. 読み書きとも `encoding=\"utf-8\"` を明示する。\n2. 出所不明のファイルは `encoding=\"utf-8-sig\"` で読む(BOM があれば除去、なければ通常通り)。\n3. 全角/半角の揺れ(ABC、カタカナ)を吸収したい場合は、比較前に `unicodedata.normalize(\"NFKC\", s)` で正規化する。\n4. `len(s)` は Unicode コードポイント数。バイト数が必要なら `len(s.encode(\"utf-8\"))`(ひらがな・漢字は 1 文字 3 バイト)。\n\n```python\nimport unicodedata\n\nTEXT = \"こんにちは、世界!全角ABCと半角カタカナの例。\"\n\nwith open(\"ja.txt\", \"w\", encoding=\"utf-8\") as f:\n f.write(TEXT)\n\nwith open(\"ja.txt\", \"r\", encoding=\"utf-8-sig\") as f:\n data = f.read()\n\nnorm = unicodedata.normalize(\"NFKC\", data)\nprint(\"文字数:\", len(data))\nprint(\"正規化後:\", norm) # ABC -> ABC、カタカナ -> カタカナ\n```"}} | |