第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

python 日期時(shí)間 strptime 格式

python 日期時(shí)間 strptime 格式

炎炎設(shè)計(jì) 2023-08-08 10:19:33
我有一個(gè)日期時(shí)間字符串,例如:"2016-08-15T07:50:12"我使用模塊strptime()中的函數(shù)datetime將字符串轉(zhuǎn)換為datetime對(duì)象。我的日期時(shí)間格式是"%Y-%m-%dT%H:%M:%S.%f"當(dāng)我解析上面的字符串時(shí),由于字符串中缺少毫秒部分,該函數(shù)會(huì)引發(fā) ValueError 。datetime當(dāng)我沒(méi)有在字符串中指定它時(shí),如何獲得毫秒為 0 的對(duì)象?
查看完整描述

3 回答

?
三國(guó)紛爭(zhēng)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

如果你得到像這樣的 ISO 8601 字符串:“2016-08-15T07:50:12”,我覺(jué)得最簡(jiǎn)單的方法是使用 dateutil 來(lái)轉(zhuǎn)換它。

import dateutil.parser
yourdate = dateutil.parser.parse(datestring)


查看完整回答
反對(duì) 回復(fù) 2023-08-08
?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

下面的代碼片段可能對(duì)您正在尋找的有幫助


from datetime import datetime

fmt='%Y-%m-%dT%H:%M:%S'

dt="2016-08-15T07:50:12"


datetime.strptime(dt, fmt)

datetime.datetime(2016, 8, 15, 7, 50, 12)


fmt1='%Y-%m-%dT%H:%M:%S.%f'

dt1="2016-08-15T07:50:12.34" 


datetime.strptime(dt1, fmt1)

datetime.datetime(2016, 8, 15, 7, 50, 12, 340000)

通過(guò)添加簡(jiǎn)單的條件語(yǔ)句你就能實(shí)現(xiàn)。


查看完整回答
反對(duì) 回復(fù) 2023-08-08
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

您需要先解析不帶時(shí)區(qū)的日期,然后再添加時(shí)區(qū)。不幸的是你需要為此子類(lèi)化 tzinfo 。


您可以使用下面的代碼并根據(jù)您的要求進(jìn)行相應(yīng)的更改。


from datetime import datetime, timedelta, tzinfo


class FixedOffset(tzinfo):

? ? """offset_str: Fixed offset in str: e.g. '-0400'"""

? ? def __init__(self, offset_str):

? ? ? ? sign, hours, minutes = offset_str[0], offset_str[1:3], offset_str[3:]

? ? ? ? offset = (int(hours) * 60 + int(minutes)) * (-1 if sign == "-" else 1)

? ? ? ? self.__offset = timedelta(minutes=offset)

? ? ? ? # NOTE: the last part is to remind about deprecated POSIX GMT+h timezones

? ? ? ? # that have the opposite sign in the name;

? ? ? ? # the corresponding numeric value is not used e.g., no minutes

? ? ? ? '<%+03d%02d>%+d' % (int(hours), int(minutes), int(hours)*-1)

? ? def utcoffset(self, dt=None):

? ? ? ? return self.__offset

? ? def tzname(self, dt=None):

? ? ? ? return self.__name

? ? def dst(self, dt=None):

? ? ? ? return timedelta(0)

? ? def __repr__(self):

? ? ? ? return 'FixedOffset(%d)' % (self.utcoffset().total_seconds() / 60)


date_with_tz = "2017-01-12T14:12:06.000-0500"

date_str, tz = date_with_tz[:-5], date_with_tz[-5:]

dt_utc = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%f")

dt = dt_utc.replace(tzinfo=FixedOffset(tz))

print(dt)

查看完整回答
反對(duì) 回復(fù) 2023-08-08
  • 3 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)