python解析变量里面的print或变量

时间:2012-07-31 11:40:01

标签: python fabric

尝试在print语句中解析变量。

client = fabric.operations.prompt("Enter client's email address:")
cmd = 'mysql -u %s -p%s -e "UPDATE users SET email = ' %(env.user, dbpasswd) + '"%client"' + ' WHERE id = 1" %s' %account
print cmd

结果:

mysql -u root -password -e "UPDATE users SET email = %client WHERE id = 1" demo

期望的结果:

mysql -u root -password -e "UPDATE users SET email = 'abc@xyz.com' WHERE id = 1" demo

2 个答案:

答案 0 :(得分:3)

我很惊讶没人提到新的字符串格式选项:

cmd = 'mysql -u {user} -p{passwd} -d "UPDATE users SET email = {email} WHERE id = 1" {account}'.format(user=env.user, passwd=dbpasswd, email=client, account=account) 

答案 1 :(得分:2)

cmd = 'mysql -u %s -p %s -e "UPDATE users SET email = ' %(env.user, dbpasswd) + "'%s'" % client + ' WHERE id = 1" %s' % account

那应该解决它。您需要将%client替换为client

的值