Hướng dẫn python change file encoding - mã hóa tệp thay đổi python
tức là, nếu bạn có một chuỗi Unicode thì hãy in nó trực tiếp. Nếu bạn có một bytestring thì trước tiên hãy chuyển nó sang Unicode. Cài đặt ngôn ngữ của bạn ( Có rất nhiều giả định sai trong câu hỏi của bạn. Bạn không cần phải thiết lập Bạn không cần giải pháp thay thế 1thì hãy sử dụng 2thay vì 3mô-đun như 4gói . 5không liên quan đến cài đặt ngôn ngữ của bạn và tới PYTHONIOENCODING . Giả định của bạn rằng cài đặt PYTHONIOENCODING sẽ thay đổi 5là không chính xác. Bạn nên kiểm tra 9thay thế. 5không được sử dụng khi bạn in ra bảng điều khiển. Nó có thể được sử dụng làm dự phòng trên Python 2 nếu stdout được chuyển hướng đến tệp / đường dẫn trừ khi 1được đặt:
Đừng gọi điện thoại 2; nó có thể làm hỏng dữ liệu của bạn một cách âm thầm và / hoặc phá vỡ các mô-đun của bên thứ 3 không mong đợi điều đó. Ghi 5được sử dụng để chuyển đổi bytestrings ( 4) đến / từ 5bằng Python 2 ngầm ví dụ 6. Xem thêm, trích dẫn trong câu trả lời của @ mesilliac .10 hữu ích 0 bình luận chia sẻ 0 bình luận chia sẻ You have stumbled over the general problem with encodings: How can I tell in which encoding a file is? Answer: You can't unless the file format provides for this. XML, for example, begins with:
This header was carefully chosen so that it can be read no matter the encoding. In your case, there is no such hint, hence neither your editor nor Python has any idea what is going on. Therefore, you must use the 3 module and use 8 which provides the missing bit in Python.As for your editor, you must check if it offers some way to set the encoding of a file. The point of UTF-8 is to be able to encode 21-bit characters (Unicode) as an 8-bit data stream (because that's the only thing all computers in the world can handle). But since most OSs predate the Unicode era, they don't have suitable tools to attach the encoding information to files on the hard disk. The next issue is the representation in Python. This is explained perfectly in the comment by heikogerlach. You must understand that your console can only display ASCII. In order to display Unicode or anything >= charcode 128, it must use some means of escaping. In your editor, you must not type the escaped display string but what the string means (in this case, you must enter the umlaut and save the file). That said, you can use the Python function eval() to turn an escaped string into a string:
As you can see, the string "\xc3" has been turned into a single character. This is now an 8-bit string, UTF-8 encoded. To get Unicode:
Gregg Lind asked: I think there are some pieces missing here: the file f2 contains: hex:
9, for example, reads them all in a separate chars (expected) Is there any way to write to a file in ASCII that would work?Answer: That depends on what you mean. ASCII can't represent characters > 127. So you need some way to say "the next few characters mean something special" which is what the sequence "\x" does. It says: The next two characters are the code of a single character. "\u" does the same using four characters to encode Unicode up to 0xFFFF (65535). So you can't directly write Unicode to ASCII (because ASCII simply doesn't contain the same characters). You can write it as string escapes (as in f2); in this case, the file can be represented as ASCII. Or you can write it as UTF-8, in which case, you need an 8-bit safe stream. Your solution using 0 does work, but you must be aware how much memory you use: Three times the amount of using 1.Hãy nhớ rằng một tệp chỉ là một chuỗi các byte với 8 bit.Cả bit và byte đều không có ý nghĩa.Chính bạn là người nói "65 có nghĩa là 'A'".Vì 2 sẽ trở thành "à" nhưng máy tính không có cách nào để biết, bạn phải nói bằng cách chỉ định mã hóa được sử dụng khi viết tệp.
|