[英]Split a string on multiple characters - Python
我有一个字符串:
V51M229D180728T132714_ACCEPT_EC_NC
这需要分为
String 1 : V51 (Can be variable but always ends before M)
String 2 : M22 (Can be variable but always ends before D)
String 3 : D180728 (Date in YYMMDD format)
String 4 : 132714 (Timestamp in HHMMSS format)
String 5 : ACCEPT (Occurs between "_")
String 6 : EC (Occurs between "_")
String 7 : NC (Occurs between "_")
我是python的新手,希望能对此有所帮助。
谢谢。
使用re
模块:
import re
a = 'V51M229D180728T132714_ACCEPT_EC_NCM'
re.search('(\w+)(M\w+)(D\d+)(T\d+)_(\w+)_(\w+)_(\w+)', a).groups()
你会得到:
('V51', 'M229', 'D180728', 'T132714', 'ACCEPT', 'EC', 'NCM')
您可能想将正则表达式与匹配的组一起使用。 请参阅re
模块。
例如,
>>> mystr = 'V51M229D180728T132714_ACCEPT_EC_NC'
>>> re.match('(.*?)(M.*?)(D.*?)T(.*?)_(.*?)_(.*?)_(.*?)', mystr).groups()
('V51', 'M229', 'D180728', '132714', 'ACCEPT', 'EC', 'NC')
在模式中, ()
表示组, .*?
将匹配最少数量的字符以使模式合适。
使用split()。 从文档:
str.split(sep =无,maxsplit = -1)
使用sep作为分隔符字符串,返回字符串中单词的列表。 如果指定了maxsplit,则最多完成maxsplit个分割(因此,列表最多包含maxsplit + 1个元素)。 如果未指定maxsplit或-1,则分割数没有限制(进行所有可能的分割)。
因此,您可以使用split('M',1)来获取['V51','229D180728T132714_ACCEPT_EC_NC']的列表,然后使用'D'分隔符拆分列表的第二个条目以获取['229','180728T132714_ACCEPT_EC_NC' ] ...
希望你能明白。
正如mxmt所说,请使用正则表达式。 这是另一个等效的正则表达式,可能更容易阅读:
import re
s = 'V51M229D180728T132714_ACCEPT_EC_NC'
pattern = re.compile(r'''
^ # beginning of string
(V\w+) # first pattern, starting with V
(M\w+) # second pattern, starting with M
(D\d{6}) # third string pattern, six digits starting with D
T(\d{6}) # time, six digits after T
_(\w+)
_(\w+)
_(\w+) # final three patterns
$ # end of string
''', re.VERBOSE
)
re.match(pattern, s).groups() -> ('V51', 'M229', 'D180728', '132714', 'ACCEPT', 'EC', 'NC')
如果您的数据是固定模式,则仅进行切片和列表切片即可。
aa = "V51M229D180728T132714_ACCEPT_EC_NC"
a = aa.split("_")
str1 = a[0][0:3]
str2 = a[0][3:6]
str3 = a[0][7:14]
str4 = a[0][15:21]
str5 = a[1]
str6 = a[2]
str7 = a[3]
print(str1,str2,str3,str4,str5,str6,str7)
产量
V51 M22 D180728 132714 ACCEPT EC NC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.