python 学习笔记
1 import :
(1)import module
(2)from module import argv
(3)from module import *
2 item :
item :把字典中的每一对 key 和 value 组成一个元组,并返回 (返回的是由字典中某一个元素的 key 和 vaule )
3 split :
split: python 中有两个split函数:
split():拆分字符串。通过指定的分隔符对字符串进行切片,并返回分割后的字符串列表(list)
os.path.split():按照路径将文件名和路径分割开。
(1) split()函数:
str . split ( 分隔符, 分割的次数) [ 选择哪一部分 ]
例子: 分割字符串
str = “www.baidu.com.cn!!!!!”
print ( str.split ( ' . ' ))
[ 'www', 'baidu' , 'com' , 'cn!!!!' ]
表示以 . 为分割符对str 进行分割
print ( str .split ( ' . ' ), 1 )
['www' , 'baidu.com.cn!!!!' ]
只分割1次, 其余的不分割 输出.
print ( str . split ( ' . ' ), 2 ) [ 0]
[ ' baidu']
分割两次, 取第几个片段
str1, str2 ,str3 = str . split ( ' . ', 2 )
print (str1) ===== www
print ( str2) ===== baidu
print (str3) ====== com.cn!!!!
(2) 分离文件名和路径:
import os
print ( os. path. split ( ' /var / panoview / modules / setup / time.js ' ))
( ' /var /panoview / modules / setup ' , ' time.js ' )
例如: str = " hello boy < [ www.daidu.com ]> bye bye"
print ( str. split ( " [" ) [ 1] . split ( " ] ") [ 0] )
www.daidu.com
4 strip :
申明: s 为字符串 , dest 为需要删除的 字符序列
s.strip ( dest ): 删除整个字符串开头 和结尾 处的 ,位于 字符序列 中的 字符
s.lstrip (dest ): 只删除开头
s.rstrip (dest) :只删除末尾
注意:
当dest 为空时, 默认删除空白符 包括 : ' \n ' , ' ' , ' \t '
例子:
str = " hello word"
str. strip ()
hello word
str = "##!!!##hello word \n 123"
str. strip ( "#!\n 123")
hello word
str. lstrip ("#!")
hello word \n 123
str. strip ("\n 123")
##!!!##hello word
5 encode :
encode 编码方式:
str . encode ( enconding = "UTF - 8 ", errors = " strict ")
enconding ---- 需要使用的编码方式
errors -------- 设置不同错误的处理方式
6 input raw_input :
这两个函数都能接收字符串,只不过 raw_inout ( )直接读取控制台的输入 ( 输入任何数据类型,都当作字符串 ) . 而 input ( ) , 它希望能够得到一个合法的 " 表达式" , 它会对输入的 "表达式" 进行计算处理,并返回结果 .
input : 参数当作 "表达式" , 自动检查数据类型, 自动进行计算处理,并返回表达式的结果
raw_input: 任何输入的参数都当作字符串处理.
注:
实质上input( )还是在使用了 raw_input ( ) 函数之后, 再调用 eval ( ) 函数.
7 type
type ( ): 接收一个对象作为参考, 并返回对象的 类型.
8 splitlines
splitlines ( ): 按照行 ( ' \ n ' , ' \r ' , ' \r \ n ' ) 分割, 返回一个包含各行所有字符的列表, 如果参数 keepends 为 false , 不包含换行符, 如果为true , 则保留换行符.
9 startwith
startwith ( ) : 用于检查字符串是否以 定义的 字符串开始, 如果是 返回 true , 否则返回 false.
str. startwith ( dest_str , beg = 0, end = len (str) )
dest_str : 表示需要检测的字符串
beg: 设置从哪里开始检测, 起始位置
end : 结束位置
10 eval
eval ( ) :
本文转自 杰思 51CTO博客,原文链接:http://blog.51cto.com/12700807/1940693