python中的字符串格式错误

时间:2012-02-26 06:13:58

标签: python-2.6 django-1.3

这是一个时间关键的错误,否则我不会在这里发布,这也是我第一次尝试使用django和python,所以请相应考虑。

我收到错误

%o format: a number is required, not str

在我的django应用程序中。它显示错误的地方:(我正在尝试创建一个消息字符串)

msg_donor = 'Dear %s,\nThank you for contributing to %s \'s fundraising campaign 
on Milaap.org. You\'ve made our day.\nRemember, since this is a loan, and not 
donation, 100% of your money will come back to you!\nYou will shortly receive 
your milaap login details. You can check who your money has gone to and track 
your repayments through your account. Be sure to sign up and check your account
regularly for updates.\n\n%s' % (d.name, c.fundraiser_name, regardsStr)

我没有在我的应用程序中写任何%o我想知道如何产生这个错误??

2 个答案:

答案 0 :(得分:1)

你的字符串中有100% of your money%是格式化字符。使用100%% of your money将文字%放在那里。

(我很惊讶Python跳过%o之间的空格,但无论如何。)

答案 1 :(得分:0)

问题是由字符串中的迷路%造成的:100%

当你进行字符串格式化时,你必须确保通过%%

来逃避文字%

试试这个:

msg_donor = """Dear %s,\nThank you for contributing to %s's fundraising campaign 
on Milaap.org. You've made our day.\nRemember, since this is a loan, and not 
donation, 100%% of your money will come back to you!\nYou will shortly receive 
your milaap login details. You can check who your money has gone to and track 
your repayments through your account. Be sure to sign up and check your account
regularly for updates.\n\n%s""" % (d.name, c.fundraiser_name, regardsStr)