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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 1970-01-01
猜你喜欢
  • 2021-08-30
  • 2021-11-28
  • 2021-06-28
  • 2021-07-30
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
相关资源
相似解决方案