python skill

作者: russelllei | 来源:发表于2016-07-12 16:37 被阅读314次

[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')

安装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里面运行没问题

相关文章

  • python skill

    [TOC] 编码报错 在python2.7下,将字符串写入到文件时会出现"UnicodeEncodeError: ...

  • SKILL技术简介

    AMAZON ALEXA SKILL What Is an Alexa Skill?Alexa is Amazon...

  • 区块链技术学习

    Blockchain Development skill basic skill: git, bash shell...

  • 机器学习基石Part1

    1. data -> ML -> skill skill: improve some performance me...

  • 【推荐】Evan-Moor批判性思维Skill Sharpene

    批判性思维Skill Sharpeners Critical Thinking图书介绍: Skill Sharpe...

  • skill

    其实,任何有价值的文字,都会有一点点难,它必然需要你思考,思考需要专注,抗拒打扰,它将产生疲惫,就像举着重物,过一...

  • skill

    I would like to acquire the skill of flying a plane. Firs...

  • 这新鲜的一天

    skill time

  • Programtic thinking

    Use Power Reading skill

  • Impala skill

    1. impala 使用concat_ws将多列合并为一列 首先查看一下表的信息 其中trim( )函数是将合并后...

网友评论

    本文标题:python skill

    本文链接:https://www.haomeiwen.com/subject/nhmydttx.html