我在正则表达式中表现不佳,请原谅我提出这个问题 我有这样的字符串:
4H71NP35P2",""
80844UNWR2",""
53UT5Z7E09",""
963ZF761A4",""
9U30538OX8",""
F1CK67N510",""
308VWHR688",""
6I5E8XKL6H",""
Y42WHYY997",""
3SF73CDEGX",""
JGQB0X133N",""
5Y5Z956O45",""
XKMXJJA822",""
Y0ZO3N28O1",""
2F59677ZX7",""
77T513NIC9",""
RH799SKGN7",""
我想在“”之间添加一个字符串,我尝试了一些正则表达式代码,但没有一个工作。
结果我希望有类似
的东西4H71NP35P2","somthing1"
80844UNWR2","somthing2"
53UT5Z7E09","somthing3"
.
.
.
感谢。
答案 0 :(得分:6)
给定大字符串,您可以使用正则表达式:
import re
s = """
4H71NP35P2",""
80844UNWR2",""
53UT5Z7E09",""
963ZF761A4",""
9U30538OX8",""
F1CK67N510",""
308VWHR688",""
6I5E8XKL6H",""
Y42WHYY997",""
3SF73CDEGX",""
JGQB0X133N",""
5Y5Z956O45",""
XKMXJJA822",""
Y0ZO3N28O1",""
2F59677ZX7",""
77T513NIC9",""
RH799SKGN7",""
"""
new_s = re.sub('""', '{}', s).format(*['"something{}"'.format(i) for i, a in enumerate(re.findall('""', s), start=1)])
输出:
4H71NP35P2","something1"
80844UNWR2","something2"
53UT5Z7E09","something3"
963ZF761A4","something4"
9U30538OX8","something5"
F1CK67N510","something6"
308VWHR688","something7"
6I5E8XKL6H","something8"
Y42WHYY997","something9"
3SF73CDEGX","something10"
JGQB0X133N","something11"
5Y5Z956O45","something12"
XKMXJJA822","something13"
Y0ZO3N28O1","something14"
2F59677ZX7","something15"
77T513NIC9","something16"
RH799SKGN7","something17"
答案 1 :(得分:4)
您可以使用replace
方法
for i,v in enumerate(a):
print v.replace('""', '"somthing{0}"'.format(i)).strip()
<强>输出:强>
4H71NP35P2","somthing0"
80844UNWR2","somthing1"
53UT5Z7E09","somthing2"
963ZF761A4","somthing3"
9U30538OX8","somthing4"
F1CK67N510","somthing5"
308VWHR688","somthing6"
6I5E8XKL6H","somthing7"
Y42WHYY997","somthing8"
3SF73CDEGX","somthing9"
JGQB0X133N","somthing10"
5Y5Z956O45","somthing11"
XKMXJJA822","somthing12"
Y0ZO3N28O1","somthing13"
2F59677ZX7","somthing14"
77T513NIC9","somthing15"