Home:ALL Converter>How to output a dictionary to stdout in csv format in python?

How to output a dictionary to stdout in csv format in python?

Ask Time:2020-10-07T14:41:04         Author:pregenRobot

Json Formatter

I have been looking for ways to outputting a dictionary to standard output in a csv format.

Dictionary has the following format

my_dict = {
  "column1": ["row1","row2","row3",...],
  "column2": ["row1","row2","row3",...],
  "column3": ["row1","row2","row3",...]
}

I have found solutions for outputting a dictionary to stdout:

Python: List of dictionaries: How to - nice printout using sys.stdout

I can always just sys.stdout every column and row in a for loop. However, I am not confident about what will happen in the edge cases (which some library will do).

I have also found solutions for outputting a dictionary to csv: How do I write a Python dictionary to a csv file?

However, these always write the dictionary to a file object instead of stdout.

My question is is there any way of outputting dictionaries to stdout in a csv format (that also handles edge cases such as: row values with , and " characters which will corrupt a csv file)?

Author:pregenRobot,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64238551/how-to-output-a-dictionary-to-stdout-in-csv-format-in-python
yy