|
@@ -89,13 +89,13 @@ So far, when we have been writing and using strings, or reading and writing to a
|
|
|
<class 'str'>
|
|
<class 'str'>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-When data is sent over the Internet, we need to send it in bytes... something your computer easily understands. The rules for translating Unicode (which is what Python uses when it stores a string) to bytes is called encoding. A popular encoding to use is UTF-8. We can read and write in UTF-8 by using a simple keyword argument in our `open function`.
|
|
|
|
|
|
|
+When data is sent over the Internet, we need to send it in bytes... something your computer easily understands. The rules for translating Unicode (which is what Python uses when it stores a string) to bytes is called encoding. A popular encoding to use is UTF-8. We can read and write in UTF-8 by using a simple keyword argument in our `open` function.
|
|
|
|
|
|
|
|
<pre><code class="lang-python">{% include "./programs/io_unicode.py" %}</code></pre>
|
|
<pre><code class="lang-python">{% include "./programs/io_unicode.py" %}</code></pre>
|
|
|
|
|
|
|
|
**How It Works**
|
|
**How It Works**
|
|
|
|
|
|
|
|
-We use io.open and then use the `encoding` argument in the first open statement to encode the message, and then again in the second open statement when decoding the message. Note that we should only use encoding in the open statement when in text mode.
|
|
|
|
|
|
|
+We use `io.open` and then use the `encoding` argument in the first open statement to encode the message, and then again in the second open statement when decoding the message. Note that we should only use encoding in the open statement when in text mode.
|
|
|
|
|
|
|
|
Whenever we write a program that uses Unicode literals (by putting a `u` before the string) like we have used above, we have to make sure that Python itself is told that our program uses UTF-8, and we have to put `# encoding=utf-8` comment at the top of our program.
|
|
Whenever we write a program that uses Unicode literals (by putting a `u` before the string) like we have used above, we have to make sure that Python itself is told that our program uses UTF-8, and we have to put `# encoding=utf-8` comment at the top of our program.
|
|
|
|
|
|