python 相关

字符串

https://www.cnblogs.com/dreamer-fish/p/3818443.html

python 打包成 exec 

利用Pycharm将Python程序生成.exe执行文件 - 知乎

python  学习

http://www.runoob.com/python/python-for-loop.html

字符与数字转换

int(x [,base ])              将x转换为一个整数    

long(x [,base ])           将x转换为一个长整数    

float(x )                      将x转换到一个浮点数    

complex(real [,imag ])  创建一个复数    

str(x )                   将对象 x 转换为字符串    

repr(x )                 将对象 x 转换为表达式字符串    

eval(str )               用来计算在字符串中的有效Python表达式,并返回一个对象    

tuple(s )                将序列 s 转换为一个元组    

list(s )                   将序列 s 转换为一个列表    

chr(x )                  将一个整数转换为一个字符    

unichr(x )              将一个整数转换为Unicode字符    

ord(x )                  将一个字符转换为它的整数值    

hex(x )                 将一个整数转换为一个十六进制字符串    

oct(x )                  将一个整数转换为一个八进制字符串   

chr(65)='A'

ord('A')=65

int('2')=2;

str(2)='2'

1、当你对shell命令的输出不感兴趣,只希望程序被运行,你可以典型的使用subprocess.call,阻塞方式
2、如果你需要捕获命令的输出结果,那么你就需要使用subprocess.Pope


http://blog.csdn.net/carolzhang8406/article/details/22286913
p = subprocess.Pope
p.wait()      //可能导致死锁

os.system(cmd)的返回值只会有0(成功),1,2

os.popen(cmd)会把执行的cmd的输出作为值返回。

1.os.popen

该命令会先创建一个管道,然后fork一个子进程,关闭管道的一端,执行exec,最后返回一个标准的io文件指针。 
popen本身是不阻塞的,要通过标准io的读取使它阻塞

2.os.system

system相当于是先后调用了fork, exec,waitpid来执行外部命令 
system本身就是阻塞的。

代码中经常会有变量是否为None的判断,有三种主要的写法:
第一种是`if x is None`;

第一种是`if x is not None`;

第二种是 `if not x:`;

第三种是`if not x is None`(这句这样理解更清晰`if not (x is None)`)

在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False ,即:

not None == not False == not '' == not 0 == not [] == not {} == not ()

因此在使用列表的时候,如果你想区分x==[]和x==None两种情况的话, 此时`if not x:`将会出现问题:

也许你是想判断x是否为None,但是却把`x==[]`的情况也判断进来了,此种情况下将无法区分。
对于习惯于使用if not x这种写法的pythoner,必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。 
而对于`if x is not None`和`if not x is None`写法,很明显前者更清晰,而后者有可能使读者误解为`if (not x) is None`,因此推荐前者,同时这也是谷歌推荐的风格

结论:
`if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。
使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。

不过这并不适用于变量是函数的情况

if foo is None: pass
if foo == None: pass

python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍_python_脚本之家

python - Is there any difference between "foo is None" and "foo == None"? - Stack Overflow

class A(object):  

    def foo(self,x):  

    #类实例方法  

        print "executing foo(%s,%s)"%(self,x)  

  

    @classmethod  

    def class_foo(cls,x):  

    #类方法  

        print "executing class_foo(%s,%s)"%(cls,x)  

  

    @staticmethod  

    def static_foo(x):  

    #静态方法  

        print "executing static_foo(%s)"%x

调用方法:

1

2

3

4

5

6

7

8

= A()  

a.foo(1)     //print   : executing foo(<__main__.A object at 0xb77d67ec>,1)

   

a.class_foo(1)    //executing class_foo(<class '__main__.A'>,1)  

A.class_foo(1)    //executing class_foo(<class '__main__.A'>,1)  

   

a.static_foo(1)    //executing static_foo(1)  

A.static_foo(1)   //executing static_foo(1)

类方法和静态方法都可以被类和类实例调用,类实例方法仅可以被类实例调用。

类方法的隐含调用参数是类,而类实例方法的隐含调用参数是类的实例,静态方法没有隐含调用参数。

python 类介绍:

http://blog.csdn.net/dreamhua927/article/details/52461816

函数访问变量:

函数中可以访问全局变量但是不能修改全局变量,除非在函数中使用  global var  再修改。在函数中使用与全局同名变量,这认为是局部变量,除非使用前使用global var ,才使用的是全局变量。函数局部变量需要申明再引用

当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值。不确定的意思可以是有,有几个,也可以是没有

*args可以当作可容纳多个变量组成的list

**kwargs可以当作容纳多个key和value的dictionary

*args称之为Non-keyword Variable Arguments

**kwargs称之为keyword Variable Arguments

def func_var_args(farg, *args):  
    print 'farg:', farg  
    for value in args:  
        print 'another arg:', value  
  
def func_var_kwargs(farg, **kwargs):  
    print 'arg:', farg  
    for key in kwargs:  
        print 'another pair arg [%s: %s]' %(key, kwargs[key])  
  
if __name__ == '__main__':
    func_var_args(1, 'two', 3)  
    print '========================================'  
    func_var_kwargs(farg=1, myarg2='two', myarg3=3)  
    print '========================================'  
    #不带参数也是可以的  
    func_var_args(1)  
    print '========================================'  
    func_var_kwargs(2)  

运行结果:

1)如何将*args和**kwargs传递到其他函数中?

直接传递下去即可,比如

def a(*args, **kwargs):

      b(*args, **kwargs)

2)如果上述入参为空,也可以正常调用,比如直接

a()

编译环境并不会抱怨没有入参

xml  包

Python’s interfaces for processing XML are grouped in the xml package.

该模块中有ElementTree  与Element  类

Element :

# <tag attrib>text<child/>...</tag>tail

tag 标记元素类别 

attrib 是一个字典

text 文本内容

tail 可选

To create an element instance, use the Element constructor or theSubElement() factory function.

ElementTree 也是一个Element

嵌套查找Element

Element has some useful methods that help iterate recursively over all the sub-tree below it (its children, their children, and so on). For example,Element.iter()

Element.findall() finds only elements with a tag which are direct children of the current element. Element.find() finds thefirst child with a particular tag, andElement.text accesses the element’s text content.Element.get() accesses the element’s attributes:

r"""Subprocesses with accessible I/O streams


This module allows you to spawn processes, connect to their
input/output/error pipes, and obtain their return codes.


For a complete description of this module see the Python documentation.


Main API
========
call(...): Runs a command, waits for it to complete, then returns
    the return code.
check_call(...): Same as call() but raises CalledProcessError()
    if return code is not 0
check_output(...): Same as check_call() but returns the contents of
    stdout instead of a return code
Popen(...): A class for flexibly executing a command in a new process


def call(*popenargs, **kwargs):
    """Run command with arguments.  Wait for command to complete, then
    return the returncode attribute.


    The arguments are the same as for the Popen constructor.  Example:


    retcode = call(["ls", "-l"])
    """
    return Popen(*popenargs, **kwargs).wait()


 

https://www.cnblogs.com/zhoug2020/p/5079407.html

python的sys.stdout重定向

http://blog.csdn.net/MTbaby/article/details/53159053

微信跳一跳

http://blog.csdn.net/mtbaby/article/details/79000399

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值