[TOC]
編碼報錯
在python2.7下,將字符串寫入到文件時會出現"UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position"的錯誤,原因是由于python基于ASCII處理字符的,當出現不屬于ASCII的字符時,會出現錯誤信息。
解決方案:
指定文件字符集為utf-8即可,在文件頭部加入以下代碼:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
編碼錯誤
數據導入pg,提示編碼錯誤,通常是在開頭加一句
import sys
sys.setdefaultencoding('utf-8')
然而,沒用
對要處理的字符串增加如下代碼,有效
astring = astring.decode('utf8', "ignore")
astring = area_id.encode('utf8')
安裝pyv8
從https://github.com/emmetio/pyv8-binaries下載對應的系統和版本的安裝包
解壓,并拷貝到對應目錄,如:
sudo mv ./* /usr/lib/python2.7/dist-packages
重啟當前的python ide即可
安裝cffi報錯
cffi依賴一個開發庫,安裝一下就可以了
sudo apt-get install libffi-dev
用sqlite,插入字符串報錯
ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings
查了參考鏈接發現,使用bytestrings時,需要給con指定模式(默認模式是text)
就是插入下面第三行即可
con = sqlite3.connect(":memory:")
cur = con.cursor()
con.text_factory = bytes
參考
https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.text_factory
conda
安裝
去官網或者清華源下載對應版本的conda,注意一下conda2和conda3對應的是py2和py3,有點兒不同
下載好,執行
bash Anaconda2-4.1.0-Linux-x86_64.sh
export PATH="/home/leisurem/anaconda/bin:$PATH"
export PATH="/home/vagrant/anaconda2/bin:$PATH"
新建環境
http://conda.pydata.org/miniconda.html
創建一個python3或者2的環境
conda create -n lei3 anaconda python=3
conda create -n lei anaconda python=2
進入環境
source activate lei3
離開環境
source deactivate
conda從rootclone env后報錯
從root里clone一個package,conda install的時候,提示
Error: 'conda' can only be installed into the root environment
這是因為環境是從root里clone過來的,把root里的一些包去掉就好了
source activate lei #假設環境叫lei
conda remove conda-build
conda remove conda-env
conda update anaconda
配置pep8
不贊同pep8的某些規范,比如e41和e501,可以在subl里跳過檢查
先在preferences的package setting里選autopepe8,選setting-user,輸入下面代碼
{
"max-line-length": 79,
// list codes for fixes; used by --ignore and --select
"list-fixes": "",
// do not fix these errors / warnings(e.g. E4, W)
"ignore": "E41, E226, E501",
// select errors / warnings(e.g. E4, W)
"select": "",
// enable possibly unsafe changes (E711, E712)
"aggressive": 0,
// number of spaces per indent level
"indent-size": 4,
"format_on_save": false,
"show_output_panel": true,
// Format/Preview menu items only appear for views
// with syntax from `syntax_list`
// value is base filename of the .tmLanguage syntax files
"syntax_list": ["Python"],
"file_menu_search_depth": 3, // max depth to search python files
"avoid_new_line_in_select_mode": false,
// print debug info into the console
"debug": false
}
然后,打開Prefrences > Package Settings > SublimeLinter > Settings - User
,然后輸入下面代碼:
{
"pep8": true,
"pep8_ignore":["E501","41"],
}
安裝pyqt5
anaconda search -t conda pyqt5
anaconda show idaholab/pyqt5
conda install --channel https://conda.anaconda.org/idaholab pyqt5
通過format格式化字符串提示‘ValueError: zero length field name in format’
python2.7之后的,不需要指定第幾個參數
但是,python2.7之前的,需要指定參數個數
比如'spark-submit --master spark://{0}:7077 /root/cm/sparm-recommand.py'.format(sp)
在python2.6和2.7都兼容
但是'spark-submit --master spark://{}:7077 /root/cm/sparm-recommand.py'.format(sp)
在python2.6會報錯,在2.7或者3里面運行沒問題
pip 無法卸載某個庫
提示It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
到lib/site-packages目錄下,把對應的庫的文件夾和庫名.egg-info文件刪除即可
比如一個pytz的庫無法卸載
sudo rm -rf pytz*
再進入目錄,刪除目錄中文件即可
conda 提示bunzip2: command not found
安裝bzip即可yum install -y bzip2
conda導入導出環境
conda env -- name lei3 export > environment.yaml
conda env create -f environment.yaml
找出list里重復最多的元素
from collections import Counter
Counter(A).most_common(1)[0][0]