如何在python(odoo)中获取09作为整数?

问题描述 投票:0回答:6

我一直在尝试这段代码,在我的代码中将九月月份设置为 09,但后来我知道 09 在 python 中是不可接受的。但它可能仅显示为字符串。我的问题是如何将 09 显示为整数?

我的代码:

    september_month = dt.datetime(int(self.year.name),09,30)   
    print september_month

错误: 无效令牌 09 和 ValueError:日期超出月份范围

这个python程序在odoo版本10 python2.7中运行

python python-2.7 python-requests odoo odoo-10
6个回答
3
投票

在 Python(任何版本)中,像

09
这样的文字被解释为八进制文字。因此
9
的值无效。

如果您确实想查看

09
,您可以编写一个字符串并使用
int("09")
转换为整数。在你的代码中:

september_month = dt.datetime(int(self.year.name),int("09"),int("30"))   
print september_month

不确定这是否真的有助于可读性。


1
投票

在 Python 3 中不可能将

int
值写为 01,02,09 等。


1
投票

伙计,它有效,你的问题是没有 9 月 31 日


1
投票

如果要显示前导 0,请将数字格式化为字符串:

"{:02d}".format(your_number)

0
投票

https://docs.python.org/3.6/library/datetime.html

Datetime 知道 9 是什么,不需要输入 09


0
投票

另一种方式:

number = 9
formatted_number = str(number).zfill(4)
print(formatted_number)  # Output: '0009'
© www.soinside.com 2019 - 2024. All rights reserved.