0

コードを動作させることができません。次のコードは、= の後と " の前にあるものを置き換える必要があります。

user = "/silent test=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 に答える