標籤:
除法相關
>>> from __future__ import division
>>> 5/3
1.6666666666666667
>>> 5/2
2.5
>>> divmod(5, 2)
(2, 1)
>>> round(5/2)
3.0
字串相關
>>> ord(‘a‘)
97
>>> chr(99)
‘c‘
編碼問題
- error---‘ascii‘ codec can‘t encode characters in position
>>> import sys
>>> sys.getdefaultencoding()
‘ascii‘
>>> sys.setdefaultencoding()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
sys.setdefaultencoding()
AttributeError: ‘module‘ object has no attribute ‘setdefaultencoding‘
>>> reload(sys)
>>> sys.setdefaultencoding(‘utf-8‘)
"AttributeError: ‘module‘ object has no attribute ‘setdefaultencoding‘" why ???
/Lib/site.py#l545:
"""Append module search paths for third-party packages to sys.path."""
* This module is automatically imported during initialization. *
def setencoding():
"""Set the string encoding used by the Unicode implementation.
The default is ‘ascii‘, but if you‘re willing to experiment, you can change this.
"""
encoding = "ascii" # Default value set by _PyUnicode_Init()
def main():
......
# Remove sys.setdefaultencoding() so that users cannot change the
# encoding after initialization. The test for presence is needed when
# this module is run as a script, because this code is executed twice.
if hasattr(sys, "setdefaultencoding"):
del sys.setdefaultencoding
源碼參見 https://hg.python.org/cpython/file/2.7/Lib/site.py#l545
編碼問題更多可參考 http://www.the5fire.com/unicodeencodeerror-from-future.html
python 常用函數