python正则表达式模块,拆分字符串,
re.split()eg:s = ‘1, 2, 3, 4‘
拆分组成数字list:strs = re.split(‘, ‘, s);
print(strs);
结果:[‘1‘, ‘2‘, ‘3‘, ‘4‘]
转成int行list:strs = list(map(int, strs));
print(strs);
结果:[1, 2, 3, 4]
如果strs中有多个分隔符,
模式串中间应用“|”分隔
各个分隔字符(或分隔字符串):eg:strs = ‘[112, 236, 372; 131]‘;
strs = re.split(‘, |; |||‘);print(strs);
结果: [‘‘, ‘112‘, ‘236‘, ‘372‘, ‘131‘, ‘‘]
去除首尾空字符串:strs.remove(‘‘);
####一次只能去除一个空串
参考:
http://www.jb51.net/article/85001.htm
https://blog.csdn.net/programmer_at/article/details/77409507?locationNum=7&fps=1
原文地址:https://www.cnblogs.com/lbwnb/p/12284440.html
时间: 2024-10-07 16:48:26