Python: How to convert a long number into string?

I was trying to transform this long number data inside tuple into a string so I can preserved its value when I send it to JSON (The file with this data inside is named b.json)

"ecc": [
        79849177969901016848164770310957289409900866592060952979899491148125256206126, 
        80012691714024297247210932953164632591330351085279802419411702853992977368435
    ]

So I try to run this command in Python

with open('b.json', 'r') as editfile:
          data2 = json.load(editfile)
      tmp = data2["ecc"]
      tmp = [tuple(str(x) for x in tup) for tup in tmp]
      data2["ecc"] = tmp

But I got this result

Traceback (most recent call last):
  File "temp-hum-log.py", line 94, in <module>
    tmp = [tuple(str(x) for x in tup) for tup in tmp]
TypeError: 'long' object is not iterable

So what should I do to convert this long number into string?

Upvotes: 0

Views: 226

Answers (1)

Azamat Galimzhanov
Azamat Galimzhanov

Reputation: 638

You went one level to deep, it's trying to iterate over this long number like it would iterate over list or string.

Try this:

with open('b.json', 'r') as editfile:
          data2 = json.load(editfile)
      tmp = data2["ecc"]
      tmp = [str(number) for number in tmp]
      data2["ecc"] = tmp

Also there is a special Decimal class for storing long number as string and you can run mathematic operations on them.

Upvotes: 1

Related Questions