Python strip() 和split() 函数
来源:互联网 发布:济南软件培训机构 编辑:程序博客网 时间:2024/05/29 19:25
1 Python strip()函数 介绍
函数原型
声明:s为字符串,rm为要删除的字符序列
s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
注意:
(1)当rm为空时,默认删除空白符(包括'\n', '\r', '\t', ' ')
(2)这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。
例如,
1
2
3
4
5
>>> a
=
' 123'
>>> a
' 123'
>>> a.strip()
'123'
(2)这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。
例如,
1
2
3
4
5
>>> a
=
'123abc'
>>> a.strip(
'21'
)
'3abc'
>>> a.strip(
'12'
)
'3abc'
结果是一样的。
2 python split()函数 介绍
说明:
Python中没有字符类型的说法,只有字符串,这里所说的字符就是只包含一个字符的字符串!!!
这里这样写的原因只是为了方便理解,仅此而已。
(1)按某一个字符分割,如‘.'
1
2
3
4
5
6
>>>
str
=
(
'www.google.com'
)
>>>
print
str
www.google.com
>>> str_split
=
str
.split(
'.'
)
>>>
print
str_split
[
'www'
,
'google'
,
'com'
]
(2)按某一个字符分割,且分割n次。如按‘.'分割1次
1
2
3
>>> str_split
=
str
.split(
'.'
,
1
)
>>>
print
str_split
[
'www'
,
'google.com'
]
(3)split()函数后面还可以加正则表达式,例如:
1
2
3
>>> str_split
=
str
.split(
'.'
)[
0
]
>>>
print
str_split
www
split分隔后是一个列表,[0]表示取其第一个元素;
1
2
3
4
5
6
>>> str_split
=
str
.split(
'.'
)[::
-
1
]
>>>
print
str_split
[
'com'
,
'google'
,
'www'
]
>>> str_split
=
str
.split(
'.'
)[::]
>>>
print
str_split
[
'www'
,
'google'
,
'com'
]
按反序列排列,[::]安正序排列
1
2
3
4
5
6
7
8
9
>>>
str
=
str
+
'.com.cn'
>>>
str
'www.google.com.com.cn'
>>> str_split
=
str
.split(
'.'
)[::
-
1
]
>>>
print
str_split
[
'cn'
,
'com'
,
'com'
,
'google'
,
'www'
]
>>> str_split
=
str
.split(
'.'
)[:
-
1
]
>>>
print
str_split
[
'www'
,
'google'
,
'com'
,
'com'
]
从首个元素开始到次末尾,最后一个元素删除掉。
split()函数典型应用之一,ip数字互换:
# ip ==> 数字
1
2
3
>>> ip2num
=
lambda
x:
sum
([
256
*
*
j
*
int
(i)
for
j,i
in
enumerate
(x.split(
'.'
)[::
-
1
])])
>>> ip2num(
'192.168.0.1'
)
3232235521
# 数字 ==> ip # 数字范围[0, 255^4]
1
2
3
>>> num2ip
=
lambda
x:
'.'
.join([
str
(x
/
(
256
*
*
i)
%
256
)
for
i
in
range
(
3
,
-
1
,
-
1
)])
>>> num2ip(
3232235521
)
'192.168.0.1'
最后,python怎样将一个整数与IP地址相互转换?
1
2
3
4
5
6
7
>>>
import
socket
>>>
import
struct
>>> int_ip
=
123456789
>>> socket.inet_ntoa(struct.pack(‘I',socket.htonl(int_ip)))
#整数转换为ip地址
‘
7.91
.
205.21
'
>>>
str
(socket.ntohl(struct.unpack(“I”,socket.inet_aton(“
255.255
.
255.255
″))[
0
]))
#ip地址转换为整数
‘
4294967295
'
阅读全文
0 0
- Python strip() 和split() 函数
- python中strip() 函数和 split() 函数
- Python strip函数和split函数
- python strip() split()函数
- python strip() split()函数
- python strip() split()函数
- 【Python】 strip(),split()函数介绍
- python中strip() 函数和 split() 函数的理解
- python strip()函数和Split函数的用法总结
- <python>strip() 函数和 split() 函数的理解
- Python学习笔记:split()函数和strip()函数的使用说明
- Python中strip() 函数和 split() 函数的理解
- python中strip() 函数和 split() 函数的理解
- <python>strip() 函数和 split() 函数的理解
- python strip()函数和Split函数的用法总结
- python strip() 函数和 split() 函数的详解及实例
- python 中的split()和strip()
- python中的strip和split
- Hive: SemanticException Error in parsing
- Mapnik使用postgres中的栅格数据
- 高级加密标准AES的工作模式(ECB、CBC、CFB、OFB)
- phpcmsv9 从第2或n条调用方法
- Vue.js 学习(4) -- 计算属性和Watchers
- Python strip() 和split() 函数
- java中总结的比较好的资源
- Json和jsonlib插件包的使用
- HTTPS 原理浅析及其在 Android 中的使用
- @Deprecated注解的含义
- Random Forest(sklearn参数详解) (转载)
- Harris算子总结
- Dagger2教程六之Component的组织方法(原)
- Linux 开启80端口访问权限