如何从JSON结果中删除数据

2024-09-29 19:23:58 发布

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

我正在使用世界时间API,希望从JSON:{"currentDateTime":"2020-07-02T18:56-07:00"}删除日期

我只想打印小时和分钟:18:56

以下是我到目前为止的情况:

url = 'http://worldclockapi.com/api/json/utc/now'
response = urllib.request.urlopen(url)
result = json.loads(response.read())
time = result['currentDateTime']

#testing to get everything here.
print(time)
Output here is: 2020-07-02T18:56-07:00

我尝试{}并使用{}格式,但它给出的时间相差一两分钟

如何使用世界时钟API打印出时区没有差异的时间

谢谢


Tags: apijsonhttpurlheretimeresponse时间
2条回答

我会对这种情况做一些字符串处理

import urllib
import requests
import json

url = 'http://worldclockapi.com/api/json/utc/now'
response = urllib.request.urlopen(url)
result = json.loads(response.read())
x= result['currentDateTime'][:-1].split('T')[-1]
print(x)

我们可以使用子字符串:

import urllib.request
import json
url = 'http://worldclockapi.com/api/json/utc/now'
response = urllib.request.urlopen(url)
result = json.loads(response.read())
time = result['currentDateTime']
print(time[-6:-1])

相关问题 更多 >

    热门问题