Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help
Special pages
SBGrid Resources
SBGrid Consortium
SBGrid Data Bank
Software Webinars
PyMOL Webinar
PyMOL Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
2to3
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= str vs. bytes = This can be the most tricky part. In Python 3, all strings are unicode, and <code>unicode</code> was renamed to <code>str</code> and <code>str</code> to <code>bytes</code>. Also, Python 2 allowed mixing of <code>unicode</code> and <code>bytes</code> and automatically encoded/decoded between them when needed. Python 3 doesn't do that, you have to encode and decode yourself, and pay attention to binary vs. text when writing files. {| class="wikitable" ! ! Python 2 ! Python 3 ! Literals (2 and 3 compatible) |- ! Unicode type | unicode | str | u"..." |- ! Binary type | str (alias bytes) | bytes | b"..." |} Code examples that works in Python 2 and 3: <syntaxhighlight lang="python"> text = u"Greek letter alpha: \u03B1" binary = u"Greek letter alpha: \u03B1".encode("utf-8") binary = b"Greek letter alpha: \xce\xb1" text = b"Greek letter alpha: \xce\xb1".decode("utf-8") </syntaxhighlight> Use binary mode when reading or writing bytes from/to files: <syntaxhighlight lang="python"> # text mode with open("file.txt", "w") as f: f.write(u"Hello World\n") f.write(b"Hello World\n".decode("utf-8")) # binary mode with open("file.bin", "wb") as f: f.write(b"Hello World\n") f.write(u"Hello World\n".encode("utf-8")) </syntaxhighlight> The [https://docs.python.org/3/library/subprocess.html subprocess] modules opens streams in binary mode, unless you pass <code>universal_newlines=True</code>: <syntaxhighlight lang="python"> # text mode p = subprocess.Popen(['echo', 'Hello World'], universal_newlines=True, stdout=subprocess.PIPE) text = p.stdout.read() # binary mode p = subprocess.Popen(['echo', 'Hello World'], stdout=subprocess.PIPE) binary = p.stdout.read() </syntaxhighlight>
Summary:
Please note that all contributions to PyMOL Wiki are considered to be released under the GNU Free Documentation License 1.2 (see
PyMOL Wiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
2to3
(section)
Add topic