您的位置:首页 > 编程语言 > Python开发

python 学习记录(1)—对内建str的处理

2012-06-21 11:30 549 查看
1.1 对str进行数据提取

In [1]:

In [2]: import subprocess

In [3]: res = subprocess.Popen(['uname','-sv'],stdout = subprocess.PIPE)

In [4]: uname = res.stdout.read().strip()

In [5]: uname

Out[5]: 'Linux #63-Ubuntu SMP Mon Nov 28 19:23:11 UTC 2011'

In [6]: '63'in uname
用in或not in检查一个字符串是不是另一个字符串的子串

Out[6]: True

In [7]: '64'in uname

Out[7]: False

In [8]: '64'not in uname

Out[8]: True

In [9]: uname.index('linux')
使用index或find找出字符串的具体位置

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

/home/lijy/<ipython console> in <module>()

ValueError: substring not found

In [10]: uname.index('Linux')

Out[10]: 0

In [11]: uname.index('Mon')

Out[11]: 21

In [12]: uname.find('Mon')

Out[12]: 21

In [13]: uname.find('mon')

Out[13]: -1

1.2 字符串切分

In [15]: smp_index = uname.index('SMP') 设置切分点位置

In [16]: smp_index

Out[16]: 17

In [17]: uname[smp_index:]

Out[17]: 'SMP Mon Nov 28 19:23:11 UTC 2011'
输出冒号右边内容

In [18]: uname[:smp_index] 输出冒号左边内容

Out[18]: 'Linux #63-Ubuntu '

In [19]: uname

Out[19]: 'Linux #63-Ubuntu SMP Mon Nov 28 19:23:11 UTC 2011'

In [20]:

1.3 判断是否以某一字符串开始或结束

In [23]: test_string = 'i am a student'

In [24]: test_string.startwith('i')

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

/home/lijy/<ipython console> in <module>()

AttributeError: 'str' object has no attribute 'startwith' 是startswith

In [25]: test_string.startswith('i')

Out[25]: True

In [26]: test_string.startswith('am')

Out[26]: False

In [27]: test_string.endswith('am')

Out[27]: False

In [28]: test_string.endswith('dent')

Out[28]: True

1.4 利用lstrip(),rstrip,strip依次删除前导空白,结尾空白,前后空白;空白可为tab,空格,回车,换行;不带参数使用时,可以返回新的字符串;

In [29]: test_string = "\n\t i am a student \n \t\r"

In [30]: tes

test2.py test_string

In [30]: test_string

Out[30]: '\n\t i am a student \n \t\r'

In [31]: print te

test2.py test_string

In [31]: print test_string

i am a student

In [32]: test_string.lstrip()

Out[32]: 'i am a student \n \t\r' 去除前方空白

In [33]: print test_string

i am a student

In [34]: print test_string.lstrip() 去除前方空白

i am a student

In [35]: print test_string.rstrip() 去除后方空白

i am a student

In [36]: test_string.rstrip()

Out[36]: '\n\t i am a student' 去除后方空白

In [37]: test_string.strip()

Out[37]: 'i am a student' 去除前后空白

In [38]: print test_string.strip()
去除前后空白

i am a student

加参数时去除特定字符

如下:

In [39]: string = '< i am a student >'

In [40]: str

str string

In [40]: string .lstrip("<")

Out[40]: ' i am a student >' 删除前一个<

In [41]: string .rstrip(">")

Out[41]: '< i am a student ' 删除后一个>

In [42]: string .strip(">")

Out[42]: '< i am a student ' 删除后一个>

In [43]: string .strip("<>")

Out[43]: ' i am a student ' 删除<>

In [44]: string .rstrip("<>")

Out[44]: '< i am a student '

In [45]: string .strip("<").strip(">")

Out[45]: ' i am a student ' 删除<>

In [46]:

In [46]: string = '< i am <><>a student >' 重新赋值

In [47]: string .rstrip("<>")

Out[47]: '< i am <><>a student '

In [48]: string .strip("<>")

Out[48]: ' i am <><>a student ' 但只能删除前后的

In [49]: string = '<foooo> i am <fooo><>a student <foooo>'

In [50]: string .strip("<>")

Out[50]: 'foooo> i am <fooo><>a student <foooo'
但只能删除前后的

In [51]: string = '<foooo> i am <fooo><>a student <foooo>'

In [52]: string .strip("<fo>")

Out[52]: ' i am <fooo><>a student '
只能删除前后的,但是删除了其它字符,说明没有按照输入字符的顺序进行匹配删除

In [53]:

1.5 利用split 根据某个指定的分隔符对一个字符串进行提取

如:

n [53]: a = "i,am,a,student,from,china"

In [54]: a.split(',')

Out[54]: ['i', 'am', 'a', 'student', 'from', 'china']

In [55]: a = "iFamFaFstudentFfromFchina"
非分隔符,对某一字符或串也有效

In [56]: a.split('F')

Out[56]: ['i', 'am', 'a', 'student', 'from', 'china']

1.6 连接多个字符串为一体

In [57]: a = ['i' 'am' 'a' 'student' 'from' 'china'] 列表

In [58]: ' '.join(a)

Out[58]: 'iamastudentfromchina' 根据空格合并

In [59]: ' '.join(a)

Out[59]: 'iamastudentfromchina'

In [60]: ''.join(a)

Out[60]: 'iamastudentfromchina'

In [61]: ' ,'.join(a)

Out[61]: 'iamastudentfromchina'

1.7替换某部分字符串

In [72]: test_string = "i am a student from china"

In [73]: te

test2.py test_string

In [73]: test_string.replace("china","USA")

Out[73]: 'i am a student from USA'

以上是使用内建的字符串类型str 进行字符串处理。

1.8 Unicode字符串

创建一个Unicode字符串



In [80]: u_string = u'this is a unicode string'
使用U 开头创建

In [81]: u_string

Out[81]: u'this is a unicode string'

In [82]: print u_string

this is a unicode string

In [83]: unicode('this is a unicode string') 使用unicode函数创建

Out[83]: u'this is a unicode string'

In [84]: a = unicode('this is a unicode string')
赋值 并输出

In [85]: a

Out[85]: u'this is a unicode string'

In [86]: print a

this is a unicode string
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: