[python] Formatting only floating numbers among Dictionary values

Asked 1 years ago, Updated 1 years ago, 125 views

I'm a beginner at Python. ;;

;;;;

I received data from two places through API and coded it with Python.

One of them is a string as shown {"A":"TEST", "B": 121212.146466}

One place is a dictionary as shown below {'A': '121212.12356, 'B': '343434.12335}

Your response has been received.

I'd like to remove a thousand commas and decimals so that only the numbers look good.

No matter how much I search on the Internet, the question below matches my intention the most, but I don't know the answer.

https://stackoverflow.com/questions/17489353/printing-a-mixed-type-dictionary-with-format-in-python

Masters, please.

python dictionary formatting

2022-09-22 13:59

1 Answers

If it is the example below, I think you can do it yourself.

A = {"A":"test", "B": 121212.146466}

For k, vin A.items(): // The result of the items is tuple and cannot be modified
    if isinstance(v, float): 
        A[k] = "{:,}".format(int(v))

# the value of A
{'A': 'test', 'B': '121,212'}


2022-09-22 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.