格式化字符串(%s)返回TypeError:需要浮点

2024-04-20 14:01:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用%s将文本插入到一个大的url中。每当我运行它时,都会出现错误:TypeError: a float is required这看起来很傻,因为我将字符串放入字符串中,没有涉及浮点或整数。任何帮助都会很棒!我的代码在下面

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比较陌生


Tags: 字符串urlbasedatetimedate错误accountfirstname
1条回答
网友
1楼 · 发布于 2024-04-20 14:01:57

这是因为您要将"/"替换为"%2F",这是Python中用于浮点的占位符。在

Use ^{} 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")

相关问题 更多 >