python skill

[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]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容