python - 格式化字符串 (%s) 返回 TypeError : a float is required

标签 python string python-3.x string-formatting

我正在尝试将文本插入带有 %s 的大 url。每当我运行它时,我都会收到错误:TypeError: a float is required 这看起来很愚蠢,因为我将字符串放入字符串中并且不涉及 float 或整数。任何帮助都是极好的!我的代码在下面

import datetime

date = (datetime.datetime.today().date() - datetime.timedelta(days=100)).strftime("%m/%d/%Y")
date = str(date)
date = date.replace("/","%2F")

def genUrl(account, date, lastName, firstName):
    url = "https://arpmp-ph.hidinc.com/arappl/bdarpdmq/pmqadhocrpt.html?a=&page=recipqry&disp-bdate=%s&disp-edate=%s&recip-list=02685245&output=web&dt-sort-by=rcpdt&mode=Recipient_Query&w_method=POST&suplist=BM0650386&base-prsb=0&base-phrm=0&base-recip=1&tot-recip=1&report=PHY&recip-sex=A&recip-dob=01%2F21%2F1964&account-id=%s&date=%s&dob-days=0&recip-name=%s&recip-fname=%s&enum-read-cnt=2&enum-keep-cnt=1&etime=5&pagename=pmqrecipqry&pdmdir=arpdm&pdmstate=ar&scriptname=%2Farappl%2Fbdarpdmq&exprecip=yes" %(date, str(datetime.datetime.now()), account, date, lastName, firstName)
    print(url)

genUrl("example",date, "Smith", "Jogn")

对不起,如果我只是犯了一个愚蠢的错误而没有注意到它。我对 Python 比较陌生

最佳答案

这是因为您要将 "/" 替换为 "%2F",这是 float 的 Python 占位符。

Use .format() instead of % :

import datetime

date = (datetime.datetime.today().date() - datetime.timedelta(days=100)).strftime("%m/%d/%Y")
date = str(date)
date = date.replace("/","%2F")

def genUrl(account, date, lastName, firstName):
    url = "https://arpmp-ph.hidinc.com/arappl/bdarpdmq/pmqadhocrpt.html?a=&page=recipqry&disp-bdate={}&disp-edate={}&recip-list=02685245&output=web&dt-sort-by=rcpdt&mode=Recipient_Query&w_method=POST&suplist=BM0650386&base-prsb=0&base-phrm=0&base-recip=1&tot-recip=1&report=PHY&recip-sex=A&recip-dob=01%2F21%2F1964&account-id={}&date={}&dob-days=0&recip-name={}&recip-fname={}&enum-read-cnt=2&enum-keep-cnt=1&etime=5&pagename=pmqrecipqry&pdmdir=arpdm&pdmstate=ar&scriptname=%2Farappl%2Fbdarpdmq&exprecip=yes".format(date, str(datetime.datetime.now()), account, date, lastName, firstName)
    print(url)

genUrl("example",date, "Smith", "Jogn")

关于python - 格式化字符串 (%s) 返回 TypeError : a float is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45911113/

相关文章:

python - 快速正则表达式 Python 3 不起作用

用于自由文本差异的 Java 库

java - 赋值的左侧必须是变量 CharAt

c# - C#中字符串前面的@是什么?

apache - 运行时错误 : populate() isn't reentrant on CentOS 7 | django 1. 9 | python 3

Python,Pairwise 'distance',需要一种快速的方法来完成

python - 定义或记录 __getattr__ 处理的调用的正确方法是什么?

Python - 将 Matplotlib 轴从几天更改为几年

python - Pandas str.replace 跳过一些替换

python - Pandas DataFrame 到嵌套 JSON 而不更改数据结构