python 在python中打印长整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/799434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:52:55  来源:igfitidea点击:

Print long integers in python

pythondjangodatetime

提问by Adam Nelson

If I run this where vote.created_on is a python datetime:

如果我运行它,其中 vote.created_on 是一个 python 日期时间:

import calendar
created_on_timestamp = calendar.timegm(vote.created_on.timetuple())*1000
created_on_timestamp = str(created_on_timestamp)

created_on_timestamp will be printed with encapsulating tick marks ('). If I do int() or something like that, I'll get something like 1240832864000L which isn't a number as far as JavaScript is concerned (which is where I need to use these datetimes).

created_on_timestamp 将使用封装的刻度线 (') 打印。如果我执行 int() 或类似的操作,我会得到 1240832864000L 之类的东西,就 JavaScript 而言,这不是一个数字(这是我需要使用这些日期时间的地方)。

Does anybody know the best way to handle this situation? Should I cast the long as a string and strip the tick marks? That seems crazy.

有人知道处理这种情况的最佳方法吗?我应该将 long 作为字符串并去除刻度线吗?这似乎很疯狂。

=== Edited Addendum ===

=== 编辑附录 ===

The larger problem was that Django was converting " into it's HTML encoded equivalent &39; (or similar). The best way to deal with this is to convert the long into a string and when the template parses the string, use {{ created_on_timestamp|safe }} to render the quote marks as quote marks.

更大的问题是 Django 正在将 " 转换为它的 HTML 编码的等效 &39; (或类似的)。解决这个问题的最好方法是将 long 转换为字符串,当模板解析字符串时,使用 {{ created_on_timestamp|safe }} 将引号呈现为引号。

回答by Bjorn

>>> i = 1240832864000L
>>> i
1240832864000L
>>> print i
1240832864000
>>> 
>>> '<script type="text/javascript"> var num = %s; </script>' % i
'<script type="text/javascript"> var num = 1240832864000; </script>'

The L only shows up when you trigger the object's __repr__

L 仅在您触发对象的 __repr__

When and how are you sending this data to JavaScript? If you send it as JSON, you shouldn't have to worry about long literals or how Python displays its objects within Python.

您何时以及如何将这些数据发送到 JavaScript?如果将其作为 JSON 发送,则不必担心长文本或 Python 如何在 Python 中显示其对象。

回答by Joe Koberg

With the line:

随着线:

created_on_timestamp = str(created_on_timestamp)

You are converting something into a string. The python console represents strings with single-quotes (is this what you mean by tick marks?) The string data, of course, does not include the quotes.

您正在将某些内容转换为字符串。python 控制台用单引号表示字符串(这就是你所说的刻度线吗?)字符串数据当然不包括引号。

When you use int()to re-convert it to a number, int()knows it's long because it's too big, and returns a long integer.

当您使用int()将其重新转换为数字时,int()因为它太大而知道它很长,并返回一个长整数。

The python console represents this long number with a trailing L. but the numeric content, of course, does not include the L.

python 控制台用尾随表示这个长数字L。但数字内容当然不包括L.

>>> l = 42000000000
>>> str(l)
'42000000000'
>>> l
42000000000L
>>> int(str(l))
42000000000L
>>> type( int(str(l)) )
<type 'long'>

Although the python console is representing numbers and strings this way (in python syntax), you should be able to use them normally. Are you anticipating a problem or have you actually run into one at this point?

尽管python控制台以这种方式表示数字和字符串(在python语法中),但您应该能够正常使用它们。你是在预料到一个问题还是你真的在这一点上遇到了一个问题?