Skip to content

Commit b625818

Browse files
committed
how to use encoding in Python when reading or writing a file
1 parent 24d3497 commit b625818

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

functions/locale_i18n/note.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## how to use encoding in Python when reading or writing a file
2+
3+
> how to use encoding in Python when reading or writing a file?
4+
5+
> a `file` can be `.py`(such as in command line), `.csv`, `.html`,`.sql`(query for a database), etc
6+
7+
[Unicode In Python, Completely Demystified (揭秘Python Unicode)](http://farmdev.com/talks/unicode/)
8+
9+
简要罗列一下最重要最实用的点:
10+
11+
Solution
12+
13+
- Decode early (尽早decode, 将"文件"中的内容转化成 unicode 再进行下一步处理)
14+
> 从文件中读出内容后,立刻解码
15+
- Unicode everywhere (程序内部处理都用unicode)
16+
>
17+
- Encode late (最后encode回所需的encoding, 例如把最终结果写进结果"文件")
18+
> 写入文件前才将内容编码
19+
20+
> see also [PYTHON处理中文的时候的一些小技巧](https://coolshell.cn/articles/461.html)
21+
22+
## Why use Unicode in Python?
23+
24+
- handle non-English languages
25+
- use 3rd party modules
26+
- accept arbitrary text input

0 commit comments

Comments
 (0)