0

trying to use input variable inside statement.

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

Error:

  File "/tmp/fabfile.py", line 196, in email
    run('mysql -u %s -p%s -e "UPDATE users SET email = %email WHERE id = 1" %s' % (env.user, dbpasswd, account))
TypeError: float argument required, not str

any idea!

4

2 に答える 2

1

%email this should be your problem,

string replacement encounters %e in %email before the last %s

In [4]: a = "1.2"

In [5]: print "%s" %a
1.2

In [6]: print "%e" %a
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Python27\<ipython-input-6-1d66dedeb226> in <module>()
----> 1 print "%e" %a

TypeError: float argument required, not str

the simplest work around would be :

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

run(cmd)

or you could also do

data = "%email"
run('mysql -u %s -p%s -e "UPDATE users SET email = %s WHERE id = 1" %s' % (env.user, dbpasswd, data, account))
于 2012-07-30T08:53:44.673 に答える
1

As said by avasal, the %email is your problem. Even if you can escape the '%' character by writing it '%%', starting from Python 2.6, I encourage you to use the new format() syntax, so you can pass it to Python 3 without any changes :

>>> template = 'mysql -u {} -p{} -e "UPDATE users SET email = %email WHERE id = 1" {}'
>>> query = template.format("user", "pass", "account")
>>> print(query)
mysql -u user -ppass -e "UPDATE users SET email = %email WHERE id = 1" account
>>> run(query)

In addition, separating template from arguments helps to avoid code duplication, which is a good practice, and can save you lot of time.

于 2012-07-30T12:48:55.117 に答える