簡體   English   中英

無法減去日期,因為IDE認為日期是unicode

[英]Can't subtract dates because IDE considers one to be unicode

我正在比較兩天,一是今天的日期,一是很大的清單。 IDE似乎認為item [3]是一個Unicode日期,因此我添加了print語句以顯示結果。 該代碼是大量代碼的一部分,但我包括了重要的部分。

碼:

print today
print item[3]
if (today - item[3]).days > 10:
        item[4] =  today + timedelta(days=10)

這是print語句的結果:

2015-01-11 00:00:00
2015-01-06

錯誤代碼:

if (today - item[3]).days > Configuration_SLA:
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'unicode'

由於item[3]是一個字符串(具有Unicode的說服力),請使用.strptime將其轉換為datetime.datetime對象:

theday = datetime.datetime.strptime(item[3], '%Y-%m-%d')
if (today - theday).days > 10:
    # etc, etc

我不認為您的IDE與它有任何關系-看起來它只是在傳遞來自Python本身的正確錯誤消息(如果您嘗試在datetime.datetime實例與任何類型的算術之間進行算術運算,則會出現該錯誤消息串!)。

順便說一句,當您想查看看起來不像它們的東西時,請務必使用

print repr(thefunnything)

這將給你更多的信息, 永遠只是

print thefunnything

其任務只是顯示人類可讀的字符串,無助於調試:-)

您看到的數字並不意味着它是datetime對象或整數。 例如,

>>> a="4"
>>> print (a)
4
>>> type(a)
<class 'str'>
>>> 

您會看到4但這並不意味着4在這里是一個整數,它是字符串。 因此,您應該檢查您的列表,像這樣調試它;

print (type(item[3]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM