FXAMN FXAMN - 1 month ago 10
Python Question

Change format of numbers inside tuples inside lists

I have this list of tuples with numbers inside, and i'm trying to change the format to:

00.000.000/0000-00


[(50195126000487.0,), (50295126000487.0,), (50395126000487.0,)]


So, the
50195126000487.0
would change to
50.195.126/0004-87
. My first thought was to iterate over the items, transform than in strings, pick everything until the dot, And then figure out how to do the rest.

Which is this code:

cnpjs_list = [(50195126000487.0,), (50295126000487.0,), (50395126000487.0,)]

for cnpj in cnpjs_list:
cnpj = cnpj[0]
print str(cnpj)


And is returning this:

5.01951260005e+13
5.02951260005e+12
5.03951260005e+13


I would like to know why this is happening, why the printing is different from what i'm seeing in the whole list? And, how to fix it?

Thanks!

Answer

Use format:

for d in [(50195126000487.0,), (50295126000487.0,), (50395126000487.0,)]:
    s = str(d[0])
    print("{}.{}.{}/{}-{}".format(s[:2], s[2:5], s[5:8], s[8:12], s[12:14]))

Output:

50.195.126/0004-87
50.295.126/0004-87
50.395.126/0004-87