使用 python 中的输入字符串格式化字典
Format Dictionary using input string in python
我正在使用以下代码从网站上抓取数据,其中三个字段由用户输入填写。
import requests
import json
URL = "http://example.com"
docs = input("Doc type: ")
fromdate = input("From: ")
todate = input("To: ")
r = requests.post(url = URL, json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":'"{}"',"FromDate":'"{}"',"ToDate":'"{}"'.format(docs,fromdate,todate)})
r.json()
但我收到这样的错误
{'exceptionMessage': 'Input string was not in a correct format.',
'exceptionType': 'System.FormatException', 'message': 'An error has
occurred.', 'stackTrace': ' at System.Number.StringToNumber(String
str, NumberStyles options, NumberBuffer& number, NumberFormatInfo
info, Boolean parseDecimal)\r\n at System.Number.ParseInt32(String
s, NumberStyles style, NumberFormatInfo info)\r\n at
BrowserView.Models.SearchCriteria.Parse()\r\n at
BrowserView.Controllers.SearchController.SearchTwo(SearchCriteria
criteria)'}
我想要下面给出的代码
json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":'"{}"',"FromDate":'"{}"',"ToDate":'"{}"'.format(docs,fromdate,todate)}
用户输入数据时这样
json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":"TAX","FromDate":"20201104","ToDate":"20201218"}
这个怎么样:
r = requests.post(url=URL, json=
{
"MaxRows": 0,
"RowsPerPage": 0,
"StartRow": 0,
"DocTypes": docs,
"FromDate": fromdate,
"ToDate": todate
})
input() 将 return 每个值作为字符串,不需要格式字符串。
如果问题仅出在将变量插入 json 中,请尝试以下操作:
r = requests.post(url=URL, json={"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":f'{docs}',"FromDate":f'{fromdate}',"ToDate":f'{todate}'})
我正在使用以下代码从网站上抓取数据,其中三个字段由用户输入填写。
import requests
import json
URL = "http://example.com"
docs = input("Doc type: ")
fromdate = input("From: ")
todate = input("To: ")
r = requests.post(url = URL, json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":'"{}"',"FromDate":'"{}"',"ToDate":'"{}"'.format(docs,fromdate,todate)})
r.json()
但我收到这样的错误
{'exceptionMessage': 'Input string was not in a correct format.', 'exceptionType': 'System.FormatException', 'message': 'An error has occurred.', 'stackTrace': ' at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\r\n at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)\r\n at BrowserView.Models.SearchCriteria.Parse()\r\n at BrowserView.Controllers.SearchController.SearchTwo(SearchCriteria criteria)'}
我想要下面给出的代码
json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":'"{}"',"FromDate":'"{}"',"ToDate":'"{}"'.format(docs,fromdate,todate)}
用户输入数据时这样
json = {"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":"TAX","FromDate":"20201104","ToDate":"20201218"}
这个怎么样:
r = requests.post(url=URL, json=
{
"MaxRows": 0,
"RowsPerPage": 0,
"StartRow": 0,
"DocTypes": docs,
"FromDate": fromdate,
"ToDate": todate
})
input() 将 return 每个值作为字符串,不需要格式字符串。
如果问题仅出在将变量插入 json 中,请尝试以下操作:
r = requests.post(url=URL, json={"MaxRows":0,"RowsPerPage":0,"StartRow":0,"DocTypes":f'{docs}',"FromDate":f'{fromdate}',"ToDate":f'{todate}'})