Learning Python
来源:互联网 发布:java if 编辑:程序博客网 时间:2024/05/18 00:33
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
dict 与 string 互转:
dict -> string : s = str(d)
string -> dict : d = eval(s)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import上层文件:
目前看到3种方式,
(1)sys.path.append("../common") (不建议,pylint会报错)
运行没问题。但是pylint会在检查import ../common目录下的文件时报错(不影响程序成功执行,影响心情)。
(2)用相对路径import的方法 (不建议,2.6之前的方式)
在每一个目录下建立一个空的__init__.py文件,然后就可以用 import .. 来找上层文件了。
(3)用绝对路径import的方法 (建议)
在每一个目录下建立一个空的__init__.py文件,import toppkg.level2pkg.level3pkg.......................
有一个不好的地方就是:路径太长。。
关于__init__.py: http://blog.csdn.net/yxmmxy7913/article/details/4233420
python导入同级别模块很方便:
import xxx
要导入下级目录页挺方便,需要在下级目录中写一个__init__.py文件
from dirname import xxx
要导入上级目录,可以使用sys.path:
首先sys.path的作用是:当使用import语句导入模块时,解释器会搜索当前模块所在目录以及sys.path指定的路径去找需要import的模块
所以改变思路,直接把上级目录加到sys.path里:sys.path.append('../')
from fatherdirname import xxx
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
disable no-self-use warning:
# pylint: disable=no-self-use
关于pylint报第三方包的错(no method, no attri), 拿lxml为例:
The reason for this is that pylint by default only trusts C extensions from the standard library and will ignore those that aren't.
As lxml isn't part of stdlib, you have to whitelist it manually. To do this, navigate to the directory of your project in a terminal, and generate an rcfile for pylint:
$ pylint --generate-rcfile > .pylintrc
Then, open that file and edit add lxml to the whitelist like so:
extension-pkg-whitelist=lxml
After that, all E1101 errors regarding lxml should vanish.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
lxml使用:
from lxml import etree
def test():
root = etree.Element('xml')
elem = etree.SubElement(root, 'sender')
elem.text = etree.CDATA('zyd')
print etree.dump(root)
print root.find('sender').text
test()
- Learning Python
- Learning Python
- Learning Python
- Learning Python
- python learning
- Learning Python
- Learning Python
- Learning Python
- python learning
- Python learning
- Learning Python
- Python Learning
- python learning
- Learning python 系列1
- Learning python 系列2
- Learning python 系列3
- Learning python 系列4
- Learning python 系列5
- 攻破JAVA NIO技术壁垒[转]
- 使用Ambari安装Hadoop集群
- 推送
- 浅谈javascript的模块化
- P2P风控措施和风控流程
- Learning Python
- ReactNative iOS 交互
- hdu2037
- MySQL(五)
- 正则正数,负数,整数,浮点数校验大全
- 不要一辈子做技术----For me
- MariaDB的主从复制、主主复制、半同步复制
- [BZOJ3670][Noi2014]动物园
- HDU 6052 To my boyfriend 期望 计数