1

I have a dictionary as follows

d={'apples':1349532000000, 'pears':1349532000000}

Doing either str(d) or repr(d) results in the following output

{'apples': 1349532000000L, 'pears': 1349532000000L}

How can I get str, repr, or print to display the dictionary without it adding an L to the numbers?

I am using Python 2.7

4

2 に答える 2

1

L接尾辞は 64 ビット整数を表すため、できません。これがないと、これらの数値は 32 ビット整数になります。これらの数値は大きすぎるため、32 ビットに収まりません。L接尾辞が省略された場合、結果は有効な Python ではなく、有効な Python を出力することが重要repr()です。

于 2012-10-07T02:57:27.363 に答える
0

まあ、これは少し恥ずかしいです、私はかなりの数の試みの後に解決策を見つけました:-P

これを行う 1 つの方法 (私の目的に適しています) は、json.dumps() を使用して辞書を文字列に変換することです。

d={'apples':1349532000000, 'pears':1349532000000}

import json
json.dumps(d)

出力

'{"apples": 1349532000000, "pears": 1349532000000}'
于 2012-10-07T03:24:37.790 に答える