Python, convert dictionary to comma separated values

Viewed 429

I need to convert the values in dict to comma separated so I can pass it to a dataclass directly.

I referred to - Link & Link

Code:

d = {'Exch': 'N', 'ExchType': 'C', 'Signal': 1660,
     'Price': 207.75, 'date': '/Date(1626690582000)/'}
print(",".join(d.keys()))
print(",".join(d.values()))

But in my case, the keys get printed, but I get an error as below for values (Note: the values in dict are of different data types):

Exch,ExchType,Signal,Price,date
    print(",".join(d.values()))
TypeError: sequence item 2: expected str instance, int found

Pardon if this query is elementary, as am quite new to Python.

1 Answers

It's because the value of the Price key is not a string.

To fix it try a list comprehension:

print(",".join([str(i) for i in d.values()]))
Related