python string解析
来源:互联网 发布:网络暴力王宝强 编辑:程序博客网 时间:2024/05/20 03:07
推荐使用str类,而不是string模块:String模块提供了常用的字符串处理函数;这些函数通常可以在str类中找到对应;
String模块内提供的某些常量还是非常有用的。
string成员常量:
-----------------------------------------------------------------------
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
octdigits = '01234567'
whitespace = '/t/n/x0b/x0c/r '
punctuation = '!"#$%&/'()*+,-./:;<=>?@[//]^_`{|}~'
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
成员函数:
-------------------------------------------------------------------------
atof(s) -> float
>>> atof = string.atof
>>> atof("3.14")
3.1400000000000001
>>> atof("1") # 可以处理整数
1.0
>>> atof("-9") # 可以处理正负号
-9.0
>>> atof("3.14f") # 不可以在数字字符尾端加‘f’
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
atof("3.14f")
File "C:/Python26/lib/string.py", line 386, in atof
return _float(s)
ValueError: invalid literal for float(): 3.14f
>>> atof("s3.14") # 遇到错误输入,抛出异常
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
atof("s3.14")
File "C:/Python26/lib/string.py", line 386, in atof
return _float(s)
ValueError: invalid literal for float(): s3.14
atoi(s [,base]) -> int
转换字符串S为整数,base默认为10。
S中最少包含一个数字,也可以带正负号。
如果base取0,则S中可以(但不必须)带有表示数制的符号:0表示八进制;0x或者0X表示十六进制;没有表示数制的符号,则表示是十进制。
如果没有指定base,则是十进制,此时S中不能加前缀0x和0X(否则抛出异常),但可以加0,此时与不加0是一样的。
如果base取16,S中也可接受前缀0x和0X(不必要)。
如果base取8,S中也可接受前缀0(不必要)
同类的有atol。
>>> atoi = string.atoi
>>> atoi("81")
81
>>> atoi("3.14") # 对于十进制数,不可接受除0~9以外的字符,因为要转换为整数,即使小数点也不可以接受;否则抛出异常
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
atoi("3.14")
File "C:/Python26/lib/string.py", line 401, in atoi
return _int(s, base)
ValueError: invalid literal for int() with base 10: '3.14'
>>> atoi("0x12") # 同上
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
atoi("0x12")
File "C:/Python26/lib/string.py", line 401, in atoi
return _int(s, base)
ValueError: invalid literal for int() with base 10: '0x12'
>>>
>>> atoi("12", 0) # 转换十进制数
12
>>> atoi("012", 0) # 转换八进制数
10
>>> atoi("0x12", 0) # 转换十六进制数
18
>>> atoi("-012", 0) # 可以接受正负号
-10
__builtins__模块内的int()、float()、long()函数的存在,使得字符串转换为数字的操作更加方便了,其语法与上述函数一致,但是形式上更加简单。
所以推荐使用int()、float()、long()等全局函数,而非string模块内的成员函数:
>>> int("1024")
1024
>>> float("3.14")
3.1400000000000001
>>> int() # 与c++很像
0
>>> float() # 同上
0.0
>>> int("1.57")
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
int("1.57")
ValueError: invalid literal for int() with base 10: '1.57'
>>> float("0x34") # float函数对参数要求要严格很多
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
float("0x34")
ValueError: invalid literal for float(): 0x34
>>> float("3.14f")
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
float("3.14f")
ValueError: invalid literal for float(): 3.14f
- python string解析
- String解析
- String解析
- String 解析
- Python - string
- Python string
- 【Python】 string
- Python---string
- python String
- python string
- python string
- String和String Pool解析
- python二进制解析strut.upack的错误unpack requires a string argument of length 4
- python二进制解析strut.upack的错误unpack requires a string argument of length 4
- python string.translate string.maketrans()
- 深度解析String对象
- String 对象的解析
- 关于String的解析
- 6/21 有道TopCoder复赛 混得过初一混不过十五......
- Linux必学的60个命令
- 保险
- DivX/XviD编解码器说明
- Java项目生成静态页面
- python string解析
- 使用SAAJ调用qq是否在线的webservice
- 第一章,数据库的启动和关闭
- axis2创建WebService
- Windows Server 2003网络负载均衡的实现
- C#中常用的分页存储过程总绪
- SQL大全------之oracle
- 又一张图片
- iPhone Developer’s Cookbook源码下载地址