我怎樣才能分散這個訊息/輸出?我試著做一些研究,但我仍然無法解決它。這是我的代碼:
@client.command()
async def userjoindate(ctx, username):
user = await roblox.get_user_by_username(username)
response = requests.get(f'https://users.roblox.com/v1/users/{user.id}')
json_data = json.loads(response.text)
message = json_data['created']
await ctx.send(message)
訊息/輸出通常是這樣的: 2012-06-28T17:54:30.74Z 我只希望它是 2012-06-28 沒有其他部分。
uj5u.com熱心網友回復:
你能得到日期字串的第一部分嗎?
message = '2012-06-28T17:54:30.74Z'
print(str(message)[0:10])
輸出:
2012-06-28
uj5u.com熱心網友回復:
你可以通過兩種方式做到這一點
一種使用 dateutil 決議器
from dateutil import parser
formattedDate = parser.parse("2012-06-28T17:54:30.74Z").strftime("%Y-%m-%d")
print(x)
和一個使用拆分(不推薦)
date = "2012-06-28T17:54:30.74Z"
print(date.split("T")[0])
uj5u.com熱心網友回復:
一個明確的方法是使用正則運算式模塊:
代碼:
import re # new import line
user = await roblox.get_user_by_username(username)
response = requests.get(f'https://users.roblox.com/v1/users/{user.id}')
json_data = json.loads(response.text)
message = json_data['created']
await ctx.send(message)
newout = re.findall("(\d -\d -\d )", oldout)[0] #new code line
輸出:
2012-06-28
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/489305.html
標籤:Python python-3.x 不和谐 不和谐.py
上一篇:Python嵌套回圈生成坐標并將它們保存到csv檔案
下一篇:從另一個類添加jpanel
