我有一个如下字符串: INPUT:tempstr =
fnameêkumarêlnameêashishêaddressêbangalore,indiaêPIN= 888888,nationalityêINDIANêcontactnumber= 888337288,emergencynum = 888222222
每个字段都假定由ê分隔。但是某些字段用“=”或“,”分隔。 我需要在python中输出如下所示。 输出:txt =
fnameêkumarêlnameêashishêaddressêbangalore,indiaêPINê888888ênationalityêINDIANêcontactnumberê888337288êemergencynumê888222222
我正在尝试替换“=”和“'”的替换函数,但它也会在地址值中替换值字段:
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
reps={", ":"ê",
"=":"ê"}
txt = replace_all(tempstr, reps)
print txt
答案 0 :(得分:0)
使用正则表达式,您可以在=
或,
后跟列表中包含的关键字时排除替换:
fname, lname, address, PIN, nationality, contactnumber,emergencynum
,只需添加所需的所有关键字。
tempstr=r'fnameêkumarêlnameêashishêaddressêbangalore, indiaêPIN=888888, nationalityêINDIANêcontactnumber=888337288, emergencynum=888222222'
outputstr = re.sub(r'[=,]\s*(?=fname|lname|address|PIN|nationality|contactnumber|emergencynum)', 'ê', tempstr)
print outputstr
<强>输出:强>
fnameêkumarêlnameêashishêaddressêbangalore, indiaêPIN=888888ênationalityêINDIANêcontactnumber=888337288êemergencynum=888222222
<强>解释强>
[=,] : = sign or comma
\s* : 0 or more spaces
(?= : start positive lookahead,
it makes sure we have one of the following keyword after
fname
|
lname
|
address
|
PIN
|
nationality
|
contactnumber
|
emergencynum
) : end lookahead
答案 1 :(得分:0)
ftxt =fnameêkumarêlnameêashishêaddressêbangalore,indiaêPIN= 888888,nationalityêINDIANêcontactnumber= 888337288,emergencynum = 888222222' out = re.sub(r“(?&lt; ==)(?P。*?)(?=,)”,r“ê\1ê”,ftxt) 打印出来
def replace_all(text,dic): for i,j in dic.iteritems(): text = text.replace(i,j) 返回文字 reps3 = {“=ê”:“ê”,“ê,”:“ê”} FOUT = replace_all(下,reps3)