文本解析常用方法

in python3 •  7 years ago 

文本解析使用的常用方法:
S.split(sep=None):
返回的是分割时候的列表,
使用sep作为分隔符来分隔字符串

s = 'hello world'
s.split(" ")
['hello', 'world']
s.split("o")
['hell', ' w', 'rld']
s.split("a")
['hello world']
s.split("l")
['he', '', 'o wor', 'd']

S.join(interable):返回值是字符串
功能:把S添加到可迭代对象中

L = ['a','b','c']
L
['a', 'b', 'c']
"".join(L)
'a
b**c'

s = 'hello'
"-".join(s)
'h-e-l-l-o'

列表推导式:
使用可迭代对象生成一个列表
是一个表达式。
语法:
[表达式 for 变量 in 可迭代对象]
[表达式 for 变量 in 可迭代对象 if 条件表达式]
说明:
1.用变量绑定从可迭代对象中取出的数据
2.变量放到表达式当中进行计算作为列表的一个元素
3.可迭代对象中没有数据取出时,列表表达式完成。
4.if可以没有,如果有if时,变量需要通过条件表达式测试真值,返回true的时候再带到表达式中计算。
5.如果,测试为False,则变量丢弃。
列表推导式的嵌套:
[表达式 for 变量1 in 可迭代对象1
for 变量2 in 可迭代对象2 if 条件表达式]

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @zhangxc! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @zhangxc! You received a personal award!

Happy Steem Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!