标签:blog log int 参数 表达式 解决 传参 logs 字符串
# 拆分含有多种分隔符的字符串
"""
实际案例:
把某个字符串依据分割符号拆分不同的字段,该字段包含多种不同的分隔符
list0 = "ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz"
其中 <,>,<;>,<|>,<\> 都是分隔符
"""
# 单一分隔符使用
s = ‘www 8596 0.0 0.0 15572 2136 pts/1 R+ 15:51 0:00 ps aux‘ print (s) # www 8596 0.0 0.0 15572 2136 pts/1 R+ 15:51 0:00 ps aux s1 = s.split() # 默认空白字符,不需要传参数 print (s1) # [‘www‘, ‘8596‘, ‘0.0‘, ‘0.0‘, ‘15572‘, ‘2136‘, ‘pts/1‘, ‘R+‘, ‘15:51‘, ‘0:00‘, ‘ps‘, ‘aux‘]
"""
解决问题方法
方法一:连续使用str.split()方法,每次处理一种分割字符
方法二:使用re模块的re.split方法,一次处理多个字符串
"""
# eg_1 使用str.split()方法
# list0 = "ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz" # # res1 = list0.split(";") # res2 = map(lambda x:x.split("|"),res1) # # t = [] # map(lambda x : t.extend(x.split("|")),res1) # # print (t)
使用函数
def mysplit(list3,ds): res = [list3] print (res) for d in ds: t = [] map(lambda x: t.extend(x.split(d)), res) res = t print (res) return res list3 = "ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz" print (mysplit(list3,";,|\t")) # [‘ab‘, ‘cd‘, ‘efg‘, ‘hi‘, ‘jkl‘, ‘mn‘, ‘opq‘, ‘rst‘, ‘uvw‘, ‘xyz‘]
使用正则表达式
import re res1 = re.split(‘[;,|\t]+‘,list3) print (res1) # [‘ab‘, ‘cd‘, ‘efg‘, ‘hi‘, ‘jkl‘, ‘mn‘, ‘opq‘, ‘rst‘, ‘uvw‘, ‘xyz‘]
标签:blog log int 参数 表达式 解决 传参 logs 字符串
原文地址:http://www.cnblogs.com/xieshengsen/p/7261204.html