A
A
AlmazKayum2021-12-26 18:51:25
Python
AlmazKayum, 2021-12-26 18:51:25

What is the correct way to convert a Python dictionary to a string?

We have a dictionary
data = {"timestamp": 1640533085532} I convert
double quotes
to a string
data = str(data)
print(data)
{'timestamp': 1640533085532}
quotes become single, how to convert to a string so that quotes are double

upd. found a way only data.replace("'",'"')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kadabrov, 2021-12-26
@Kadabrov

can be unpacked using a loop and get the key + value as a string

list_data = []
data = {"timestamp": 1640533085532}
for k,  v in data.items():
    list_data.append(k + ':' + str(v))

you can also unpack into sets and get a list with sets of keys and values
new_data = data.items()

P
Python Newbie, 2021-12-26
@Levman5

https://ru.stackoverflow.com/questions/1043473/%D0...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question