python:从两个分隔符之间的一行中选择字符串,并与同一行中的其他字符串一起打印

时间:2017-04-26 14:51:41

标签: python string replace

我有一个如下字符串: 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

2 个答案:

答案 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)