如何将来自 Postman 的 Json 数据转换为 python 中的整数?
How can I convert Json data that coming from Postman convert into Integers in python?
我在这里使用 MQL5 编写了一个 python 脚本以从 MetaTrader5 获取总历史订单。这是代码,
from_date = datetime(2020, 1, 1)
to_date = datetime.now()
history_orders = mt.history_orders_total(from_date, to_date )
print(history_orders)
我的要求是我需要从用户那里获取 from_date 和 to_date 参数。所以我使用 POST 从 Postman 传递了这些参数 request.here 是 payload
{
"fromDate" : "2020, 1, 1",
"toDate": "2020, 11, 8"
}
它说,
history_orders = mt.history_orders_total(datetime(fromDate),
datetime(toDate)) TypeError: an integer is required (got type str)
如何将这些 Json 有效负载转换为 python 中的整数?
这是我的代码。
@app.route("/", methods=['POST', 'GET'])
def historyOrderTotal():
fromDate = None
toDate = None
if request.method == 'POST':
fromDate = request.json['fromDate']
toDate = request.json['toDate']
mt.initialize()
history_orders = mt.history_orders_total(datetime(fromDate), datetime(toDate))
print(history_orders)
你不太关心整数,你想要 datetime
个对象。您已决定要 <year>, <month>, <day>
作为日期的传入序列化 JSON 格式。
所以你可能会这样做:
from datetime import datetime
pattern = "%Y, %m, %d"
from_date = datetime.strptime(fromDate, pattern)
to_date = datetime.strptime(toDate, pattern)
您可能想要添加验证,例如assert from_date <= to_date
等,具体取决于您的用例。
我还建议您查看 ISO 8601 日期格式,这是一种更标准化的日期序列化方式。
调试说明,根据您的意见试试这个:
@app.route("/", methods=['POST', 'GET'])
def historyOrderTotal():
fromDate = None
toDate = None
if request.method == 'POST':
fromDate = request.json['fromDate']
toDate = request.json['toDate']
print("json-from:", fromDate)
print("json-to:", toDate)
pattern = "%Y, %m, %d"
from_date = datetime.strptime(fromDate, pattern)
to_date = datetime.strptime(toDate, pattern)
print("parsed-from:", from_date)
print("parsed-to:", to_date)
if mt.initialize():
history_orders = mt.history_orders_total(from_date, to_date)
print("history-orders", history_orders)
else:
print("initialize() failed, error code =", mt.last_error())
else:
print(">> Not a POST request")
我在这里使用 MQL5 编写了一个 python 脚本以从 MetaTrader5 获取总历史订单。这是代码,
from_date = datetime(2020, 1, 1)
to_date = datetime.now()
history_orders = mt.history_orders_total(from_date, to_date )
print(history_orders)
我的要求是我需要从用户那里获取 from_date 和 to_date 参数。所以我使用 POST 从 Postman 传递了这些参数 request.here 是 payload
{
"fromDate" : "2020, 1, 1",
"toDate": "2020, 11, 8"
}
它说,
history_orders = mt.history_orders_total(datetime(fromDate), datetime(toDate)) TypeError: an integer is required (got type str)
如何将这些 Json 有效负载转换为 python 中的整数? 这是我的代码。
@app.route("/", methods=['POST', 'GET'])
def historyOrderTotal():
fromDate = None
toDate = None
if request.method == 'POST':
fromDate = request.json['fromDate']
toDate = request.json['toDate']
mt.initialize()
history_orders = mt.history_orders_total(datetime(fromDate), datetime(toDate))
print(history_orders)
你不太关心整数,你想要 datetime
个对象。您已决定要 <year>, <month>, <day>
作为日期的传入序列化 JSON 格式。
所以你可能会这样做:
from datetime import datetime
pattern = "%Y, %m, %d"
from_date = datetime.strptime(fromDate, pattern)
to_date = datetime.strptime(toDate, pattern)
您可能想要添加验证,例如assert from_date <= to_date
等,具体取决于您的用例。
我还建议您查看 ISO 8601 日期格式,这是一种更标准化的日期序列化方式。
调试说明,根据您的意见试试这个:
@app.route("/", methods=['POST', 'GET'])
def historyOrderTotal():
fromDate = None
toDate = None
if request.method == 'POST':
fromDate = request.json['fromDate']
toDate = request.json['toDate']
print("json-from:", fromDate)
print("json-to:", toDate)
pattern = "%Y, %m, %d"
from_date = datetime.strptime(fromDate, pattern)
to_date = datetime.strptime(toDate, pattern)
print("parsed-from:", from_date)
print("parsed-to:", to_date)
if mt.initialize():
history_orders = mt.history_orders_total(from_date, to_date)
print("history-orders", history_orders)
else:
print("initialize() failed, error code =", mt.last_error())
else:
print(">> Not a POST request")