---
disqus: jexus-md
---
Python Notes
===
## 1. input( )
>Python 3.any's input( ) works like 2.any's raw_input(), i.e., it returns a string.
## 2. sys.argv[] : 命令提示字元引數
```python
>>> python somefile.py somewords
//sys.argv[1]==somewords
```
>https://openhome.cc/Gossip/Python/IOABC.html
## 3. exit( )們 :
### exit()/quit()
拋出 SystemExit 異常. 一般在交互式 Shell 中退出時使用.
### sys.exit(n)
退出程序引發 SystemExit 異常, 可以捕獲異常執行些清理工作. n 默認值為 0, 表示正常退出. 其他都是非正常退出. 還可以 sys.exit(“sorry, goodbye!”); 一般主程序中使用此退出.
### os._exit(n)
直接退出, 不拋異常, 不執行相關清理工作. 常用在子進程的退出.
>http://jaminzhang.github.io/python/exit-and-sys-exit-in-python/
## 4. 字串處理精解 :
>http://itman2266.blogspot.tw/2013/04/python_22.html
## 5. 去掉 unicode 字串前面的 u :
```python
In [4]: u'\xe4\xbd\xa0\xe5\xa5\xbd'.encode('raw_unicode_escape')
Out[4]: '\xe4\xbd\xa0\xe5\xa5\xbd'
In [5]: u'\xe4\xbd\xa0\xe5\xa5\xbd'.encode('raw_unicode_escape').decode('utf8')
Out[5]: u'\u4f60\u597d'
In [7]: print u'\u4f60\u597d'
你好
```
>https://mozillazg.github.io/2013/12/python-raw-unicode.html
## 6. UnicodeDecodeError: 解決方法 :
- 1.全都轉byte string
```python
self.response.out.write("你好"+self.request.get("argu").encode('utf-8'))
```
- 2.全都轉unicode string
```python
self.response.out.write(u"你好"+self.request.get("argu"))
```
>P.S.資料庫存入和讀取以及self.request拿到的參數預設就都是unicode string,若是要把byte string轉unicode string可以這樣轉unicode(unicodestring,"utf-8")
>這樣就再也不會有
>`"UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)"`
>的問題產生了 遇到噴出這種錯誤幾乎都可以用這2種方法解決
`print p1.extractText().encode('utf8')`
>http://blog.wahahajk.com/2009/08/unicodedecodeerror-ascii-codec-cant.html
## 7. eval( ) --txt讀完檔案可以轉成list之類有用的值,不然string沒辦法直接用 :
>內建函數 (function) eval() ,參數 (parameter) expression 為具有可執行運算式的字串,此函數執行 expression 中的運算式
## 8. 原來raw_input( )吃的是Big5 ! :
解決方法
```python
name = raw_input()
print name.decode("big5")
```
註 : 由flask的url取得的中文字串完全不需重新編碼,已經是utf8了
## 9. 正規表示式_中文解法 :
```python
reg=re.compile(r'(^[\u4e00-\u9fa5]+)-([\u4e00-\u9fa5]+$)')
reg.search(u'錦繡-復興北村'.encode('raw_unicode_escape')).group()
```
### BUT... 雙反斜線問題QQ
```python
>>> reg=re.compile(r'([\u4e00-\u9fa5]+)-([\u4e00-\u9fa5]+)')
>>> reg.search('\u5566-\u5566').group()
'\\u5566-\\u5566'
>>> reg.search(u'錦繡-復興北村'.encode('raw_unicode_escape')).group()
'\\u9326\\u7e61-\\u5fa9\\u8208\\u5317\\u6751'
>>> reg.search(u'錦繡-復興北村'.encode('raw_unicode_escape')).group(1)
'\\u9326\\u7e61'
>>> ans=reg.search(u'錦繡-復興北村'.encode('raw_unicode_escape')).group()
>>> ans
'\\u9326\\u7e61-\\u5fa9\\u8208\\u5317\\u6751'
>>> ans.encode('utf8')
'\\u9326\\u7e61-\\u5fa9\\u8208\\u5317\\u6751'
>>> ans.decode('utf8')
u'\\u9326\\u7e61-\\u5fa9\\u8208\\u5317\\u6751'
>>> print ans.decode('utf8')
>>> ans=reg.search(ur'錦繡-復興北村'.encode('raw_unicode_escape')).group()
>>> print ans.decode('utf8')
\u9326\u7e61-\u5fa9\u8208\u5317\u6751
>>> ans.decode('utf8')
u'\\u9326\\u7e61-\\u5fa9\\u8208\\u5317\\u6751'
```
結論 : 還無法解決QQ
直接用for i in <string>:來找好像比較快QQ
## 10. TypeError: 'int' object has no attribute '\_\_getitem\_\_'
雙重迴圈中若不慎使用同一個i作為iterative,會導致在小迴圈結束之後的i值或是屬性改變,再回到大迴圈時出現像這樣的error
## 11. PyPDF2 -> mergePage
```python
>>> www=wm.getPage(0)
>>> p1.mergePage(www)
>>> qq.addPage(p1)
# -> 分開弄可以
>>> qq.addPage(p1.mergePage(www))
# -> 但請不要合在同一行,會挫屎
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Jexus\Documents\MIT\newenv\lib\site-packages\PyPDF2\pdf.py", line 138, in addPage
self._addPage(page, list.append)
File "C:\Users\Jexus\Documents\MIT\newenv\lib\site-packages\PyPDF2\pdf.py", line 123, in _addPage
assert page["/Type"] == "/Page"
TypeError: 'NoneType' object has no attribute '__getitem__'
```
## 12. cProfile.run
>https://zwindr.blogspot.tw/2016/08/python-cprofile.html
>
## 13. OpenSSL.SSL.Error:
解決方法
```python
from backports import ssl
from imapclient import IMAPClient
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
imapObj = IMAPClient('imap.gmail.com', ssl=True, ssl_context=context)
```
>https://stackoverflow.com/questions/34780726/imapclient-error-diff-version-python-and-windows
>
## 14.Flusk佈署到內網
調用的時候一般是`app.run()`
但是這樣默認的地址是`http://127.0.0.1:5000`只能本機訪問
可以這樣調用`app.run(host='192.168.1.2')`
host裡面要打真實內網ip位置
這樣訪問地址是`http://192.168.1.2:5000`
>https://windard.com/project/2016/12/01/Deploy-Flask-APP
>https://www.phpbulo.com/archives/900.html
>
## 15.工作站無法sudo又沒裝pip
```shell
wget https://github.com/pypa/virtualenv/archive/15.0.3.tar.gz
tar xvf 15.0.3.tar.gz
python3 15.0.3/virtualenv.py venvname
```
> https://github.com/vinaymodi/notes/wiki/Python-virtualenv-without-sudo
>
## 16.tensorflow看GPU
```python
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
# cpu
cat /proc/cpuinfo
```
## 17. dict setdefault
```python=
dictionary = {}
dictionary.setdefault("list", []).append("list_item")
```
## 18.print sorted dict
```python
dict([(k,di[k]) for k in sorted(di)])
```
## 19.Cuda runtime error (59)
```
RuntimeError: cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCStorage.cpp:36
```
非常可能是 loss function 弄 cross-entropy 的時候,index 超出範圍了
##
<p><div style="font-size:0.8em">查看我的其他筆記 — — <a href="https://hackmd.io/@voidism">回到 Jexus.md 首頁</a></div></p>