2017-04-14 12 views
0

私はいくつかのjsonを返す次のコードを用意しています。出力を.subample.comに変更したい。通常はawkでこれを行うことができますが、この特殊なケースではPythonで処理する必要があります。Pythonの文字列代入

私がやっていることは、リテラル文字列 'example.com'を 'sub.example.com'に置き換えることです。 IPビット作品アウトフィルタリングが、私はちょうどより簡単一部:(どうあるべきかを把握することはできません。

def filterIP(fullList): 
    regexIP = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$') 
    return filter(lambda i: not regexIP.search(i), fullList) 

def filterSub(fullList2): 
    regexSub = re.recompile('example.com', 'sub.example.com', fullList2) 

groups = {key : filterSub(filterIP(list(set(items)))) for (key, items) in groups.iteritems() } 

print(self.json_format_dict(groups, pretty=True)) 

    "role_1": [ 
    "type-1.example.com", 
    "type-12-sfsdf-453-2.example.com" 
    ] 
+0

正規表現で '.'をエスケープする必要があります。 – Barmar

+0

're.recompile'関数はありません。 – Barmar

+3

're.sub()'を呼び出さない – Barmar

答えて

0

filterSub()re.sub()を呼び出して結果を返す必要があります。また、正規表現で.をエスケープする必要があります。。、それは特別な意味を持っているので、そして、あなたは唯一のドメイン名の最後にそれを一致させるよう$アンカーを使用

def filterSub(fullList2): 
    return re.sub(r'example\.com$', 'sub.example.com', fullList2) 
0

このために正規表現を使用する理由はありません:それは単純な文字列を置き換えるだ

def filterSub(fullList2): 
    return fullList2.replace("example.com", "sub.example.com") 
+0

私の問題は、実際に私が扱っている文字列ではないようです。辞書のエントリです。 –

関連する問題