码迷,mamicode.com
首页 > 编程语言 > 详细

python笔记一

时间:2015-12-18 22:41:18      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

1、编码转换

   一般硬盘存储为utf-8,读入内存中为unicode,二者如何转换

      a = ‘你好‘              ‘\xe4\xbd\xa0\xe5\xa5\xbd‘     <type ‘str‘>

      b = u‘你好‘             u‘\u4f60\u597d‘                <type ‘unicode‘>

      a.decode(‘utf-8‘)       u‘\u4f60\u597d‘   (utf-8格式解码为unicode)

      b.encode(‘utf-8‘)       ‘\xe4\xbd\xa0\xe5\xa5\xbd‘  (unicode格式加密为utf-8)

    注:在python2.7版本中需要如上转换,在脚本中如要显示中文,

        只要在文件开头加入 # _*_ coding: UTF-8 _*_ 或者 #coding=utf-8 就行了

        在python3.4以后版本,无需转换

 

2、调用系统命令,并存入变量:

    1.import os

    a = os.system(‘df -Th‘)

    b = os.popen(‘df -Th‘,‘r‘) 返回一个文件对象

   

    2.import commands

    c = commands.getoutput(‘df -Th‘)  返回一个字符串

 

3、sys调用

    import sys

    sys.exit

    print sys.arg

    sys.path

 

4、导入模板方法:

      1.import sys [as newname]  

    多次重复使用import语句时,不会重新加载被指定的模块,只是把对该模块的内存地址给引用到本地变量环境。

      2.from sys import argv或(*)

      3.reload()

    reload会重新加载已加载的模块,但原来已经使用的实例还是会使用旧的模块,而新生产的实例会使用新的模块;reload后还是用原来的内存地址;不能支持from。。import。。格式的模块进行重新加载。

   建议使用第一种,第二种导入的对象或变量会与当前的变量会冲突。

 

5、用户交互:

     在python2.7版本中

        raw_input:交互输入内容转化为字符串;

        input:交互输入内容不进行转化;

     在python3.4版本中只有raw_input,想要获取数字,需要进行int转变。

     举例:

技术分享
 1 #_*_ coding:utf-8 _*_
 2 
 3 info = This var will be printed out ...
 4 name = raw_input(Please input your name:)
 5 age = int(raw_input(age:))   
 6 #age = input(‘age:‘)
 7 job = raw_input(Job:)
 8 salary  = input(Salary:)
 9 print type(age)
10 
11 print ‘‘‘
12 Personal information of %s:
13           Name: %s 
14           Age : %d
15           Job : %s
16         Salary: %d
17 --------------------------
18 ‘‘‘ % (name,name, age,job,salary)
View Code

 

6、文件操作:   

   python2.7版本中可以用file和open打开文件,  python3.4版本中只有open

       f = open(‘file_name‘,‘r‘)

       g = file(‘file_name‘,‘r‘)  

   其中打开模式有‘r‘,‘w,‘,‘a‘,‘b‘,‘+‘

       w:替换重写   a:追加  

       b:二进制文件,主要用于跨平台,来解决window和linux的回车换行区别

       +:用于同时读写

   * 一般会对文件读到的第一行去掉末尾的换行符      f.readline().strip(‘\n‘)

   * xreadlines:针对大文件,一行一行读,默认是把全文件读入内存。

   * r+ :读写,默认从文件尾写入,可以由seek跳到指定位置,然后替换文件内容。


    例:f = open(‘file_name‘,‘r+‘)

        f.seek(50)

        a = range(10)

        a = [ str(i) + ‘\n‘ for i in a ]

        f.writelines(a)


   

7、类型转变:

 

Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数。

函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式(如果没有等价的
语法,则会发生SyntaxError 异常) 某对象没有适于人阅读的解释形式的话, str() 会返回与repr()等同的值。很多类型,诸如数值或链表、字典这样的结构,针对各函数都有着统一的解读方式。字符串和浮点数,有着独特的解读方式。
Some examples:


下面有些例子
>>> s = ‘Hello, world.‘
>>> str(s)
‘Hello, world.‘
>>> repr(s)
"‘Hello, world.‘"
>>> str(1.0/7.0)
‘0.142857142857‘
>>> repr(1.0/7.0)
‘0.14285714285714285‘


 

8、字典复制:

dict = {‘name‘:‘wang‘, ‘sex‘:‘m‘, ‘age‘:34, ‘job‘:‘it‘}

info = dict            ##别名 (二个字典指向内存的同一地址空间)

info1 = dict.copy()    #shadow copy 浅复制(嵌套字典第一层独立,第二层以下相关联)

import copy

copy.copy()            #shadow copy 浅复制

copy.deepcopy()        #deep copy   深复制(完全独立)

注:浅复制下的关联只是针对字典初始状态包含的嵌套对象,后新加的不会

例:


>>> dict
{‘info‘: [‘a‘, ‘b‘, 1, 2], ‘job‘: ‘it‘, ‘sex‘: ‘m‘, ‘age‘: 40, ‘name‘: ‘wang‘}
>>> dict_alias = dict
>>> dict_copy = copy.copy(dict)
>>> dict_deep = copy.deepcopy(dict)


#添加、改变、删除第一层的对象键值,浅复制和深复制都不受影响

>>> dict[‘age‘] = 32

>>> del dict[‘sex‘]
>>> dict
{‘info‘: [‘a‘, ‘b‘, 1, 2], ‘job‘: ‘it‘, ‘age‘: 32, ‘name‘: ‘wang‘}
>>> dict_alias     
{‘info‘: [‘a‘, ‘b‘, 1, 2], ‘job‘: ‘it‘, ‘age‘: 32, ‘name‘: ‘wang‘}
>>> dict_copy      
{‘info‘: [‘a‘, ‘b‘, 1, 2], ‘job‘: ‘it‘, ‘age‘: 40, ‘name‘: ‘wang‘, ‘sex‘: ‘m‘}
>>> dict_deep      
{‘info‘: [‘a‘, ‘b‘, 1, 2], ‘job‘: ‘it‘, ‘age‘: 40, ‘name‘: ‘wang‘, ‘sex‘: ‘m‘}


#改变、删除原有的第二层的对象键值,浅复制受影响,而深复制都不受影响

>>> dict[‘info‘][2] = 100
>>> dict
{‘info‘: [‘a‘, ‘b‘, 100, 2], ‘job‘: ‘it‘, ‘age‘: 32, ‘name‘: ‘wang‘}
>>> dict_alias
{‘info‘: [‘a‘, ‘b‘, 100, 2], ‘job‘: ‘it‘, ‘age‘: 32, ‘name‘: ‘wang‘}
>>> dict_copy
{‘info‘: [‘a‘, ‘b‘, 100, 2], ‘job‘: ‘it‘, ‘age‘: 40, ‘name‘: ‘wang‘, ‘sex‘: ‘m‘}
>>> dict_deep
{‘info‘: [‘a‘, ‘b‘, 1, 2], ‘job‘: ‘it‘, ‘age‘: 40, ‘name‘: ‘wang‘, ‘sex‘: ‘m‘}


#添加第二层的对象,浅复制和深复制都不受影响

>>> dict[‘new‘] = {‘a‘:1, ‘b‘:2, ‘c‘:5}
>>> dict
{‘info‘: [‘a‘, ‘b‘, 100, 2], ‘name‘: ‘wang‘, ‘age‘: 32, ‘job‘: ‘it‘, ‘new‘: {‘a‘: 1, ‘c‘: 5, ‘b‘: 2}}
>>> dict_alias
{‘info‘: [‘a‘, ‘b‘, 100, 2], ‘name‘: ‘wang‘, ‘age‘: 32, ‘job‘: ‘it‘, ‘new‘: {‘a‘: 1, ‘c‘: 5, ‘b‘: 2}}
>>> dict_copy
{‘info‘: [‘a‘, ‘b‘, 100, 2], ‘job‘: ‘it‘, ‘age‘: 40, ‘name‘: ‘wang‘, ‘sex‘: ‘m‘}
>>> dict_deep
{‘info‘: [‘a‘, ‘b‘, 1, 2], ‘job‘: ‘it‘, ‘age‘: 40, ‘name‘: ‘wang‘, ‘sex‘: ‘m‘}


 

9、内置函数说明:

      __name__:主文件时返回main,否则返回文件名,可用来判断是否说主文件还是导入模块;

      __file__:文件的绝对路径;

      __doc__:文件开头的注释说明

例:

‘‘‘
    created by 2015-05-24
    @author: kevin
‘‘‘


if __name__ == ‘__main__‘:
    print(‘this is main file‘)
    print(__file__)
    print(__doc__)

python笔记一

标签:

原文地址:http://www.cnblogs.com/wushank/p/5058340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!