0

无法使代码正常工作,以下代码应替换 = 之后和之前的内容

用户=“/静默测试=de”;

#!/usr/bin/python
import re
import fileinput

langids = ["de","uk","us","dk"]

for lang in langids:
 for line in fileinput.FileInput("C:\Users\HS\Desktop\sps_r2850\Test.txt",inplace=1):
    if re.match(".*user = \"[a-zA-Z/_= ]*\";.*", line):
        line = ("user = \"/silent test=_%s\";\n" % lang)
    print line,
4

1 回答 1

3

使用更改语言re.sub

import re

# no value set (or any doesn't matter)
line='user = "/silent test=";'

# values to insert 
langids = ["de","uk","us","dk"]

for lang in langids: 
     print re.sub('(.*=)(.*)(".*)',r'\1'+lang+r'\3',line)

# output
>>> user = "/silent test=de";
>>> user = "/silent test=uk";
>>> user = "/silent test=us";
>>> user = "/silent test=dk";
于 2012-11-15T08:31:08.257 回答