2 回答
TA贡献1836条经验 获得超4个赞
使用str.replace()with的示例str.split():
s = ['126;AOC;H3498XX from 602', '101;KYL;0IMMM0432 from 229']
for elem in s:
print(elem.replace(elem.split(";",2)[-1].split()[0],''))
输出:
126;AOC; from 602
101;KYL; from 229
编辑:
同样适用于以下示例:
s = ['Received value 126;AOC;H3498XX from 602', 'Received value 101;KYL;0IMMM0432 from 229']
for elem in s:
print(elem.replace(elem.split(";",2)[-1].split()[0],''))
输出:
Received value 126;AOC; from 602
Received value 101;KYL; from 229
TA贡献1829条经验 获得超9个赞
使用模式 (Received value \d+;[A-Z]+;)\w+(\s.*?)
前任:
import re
s = ["Received value 126;AOC;H3498XX from 602", "Received value 101;KYL;0IMMM0432 from 229"]
for i in s:
print( re.sub(r"(Received value \d+;[A-Z]+;)\w+(\s.*?)", r"\1", i) )
输出:
Received value 126;AOC;from 602
Received value 101;KYL;from 229
添加回答
举报