我编写了一个 python 脚本,将所有版本转储到一个文本文件中。所有版本均以“|”分隔 象征。
我需要用以下条件替换所有以 3 开头的版本
例如 1) 3.7.0E 应替换为 03.07.00E 2) 3.17.1E 应替换为 03.17.01E
所有单数数字都应替换为 0
3.7.0E | 3.7.1E | 3.7.2E | 3.7.3E | 3.7.4E | 3.7.5E | 16.2.1 | 16.2.2 | 3.8.0E | 16.3.1 | 16.3.2 | 16.3.3.3. | 1a | 16.4.1 | 16.4.2 | 3.17.1E | 3.7.11E
这并不漂亮,但它会做你想做的事:
import re
s = '3.7.0E|3.7.1E|3.7.2E|3.7.3E|3.7.4E|3.7.5E|16.2.1|16.2.2|3.8.0E|16.3.1|16.3.2|16.3.3|16.3.1a|16.4.1|16.4.2|3.17.1E|3.7.11E'
l = []
# split up based on pipe
for chunk in s.split('|'):
if chunk.startswith('3'):
new_chunk = ''
# split up based on period
for piece in chunk.split('.'):
try:
# if there's a letter, exception will be thrown
x = int(piece)
new_chunk += '0{}.'.format(x) if x < 10 else '{}.'.format(x)
except:
n = int(re.search('\d+', piece).group(0))
letter = re.search('\w', piece).group(0)
new_chunk += '0{}{}'.format(n, letter) if n < 10 else piece
l.append(''.join(new_chunk))
else:
l.append(chunk)
new_s = '|'.join([p for p in l])
print(new_s)
new_s
will的值是:'03.07.00E|03.07.01E|03.07.02E|03.07.03E|03.07.04E|03.07.05E|16.2.1|16.2.2|03.08.00E|16.3.1|16.3.2|16.3.3|16.3.1a|16.4.1|16.4.2|03.17.01E|03.07.11E'
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句