【发布时间】:2014-06-19 13:21:40
【问题描述】:
所以我有这个简单的代码:
def modify_mysql_log():
#/var/log/mysql/mysql.log
#Change: queries
f=open("../mysql/mysql.log")
t=open("../mysql/mysql2.log","w")
for line in f:
line=re.sub(r'Query\s*SELECT[A-Za-z\s+\W_]*\'',random.choice(queries),line)
t.write(line)
f.close()
t.close()
现在我想做的是,我希望能够用我用正则表达式找到的查询替换我曾经搜索过的正则表达式,并结合 random.choice(queries) 和正则表达式结果。我查看了其他 re 函数,但不知何故我不知道该怎么做。
【问题讨论】:
-
你试过像
"%s" % ("text")或"{}".format("text")这样的字符串格式化函数吗?
标签: python regex string substitution