Dec-27-2022, 08:24 PM
I really messed up my previous post during an edit. No wonder you couldn't figure out what it was supposed to do.
Sorry about that. The code should look like this:
Hope this is clear now.
Sorry about that. The code should look like this:
import pandas as pd odd_stuff = pd.DataFrame([[1, '1', 'one', None, ''], [2, '2', '', None, None]]) def df_to_csv(dataframe, csv_file, sep=','): """Write dataframe to csv file using special conventions. 1: All values except None are surrounded by double quotes. 2: None will appear as a blank. """ with open(csv_file, "w") as file: for row in dataframe.values: row_str = sep.join(['' if x is None else f'"{x}"' for x in row]) print(row_str, file=file) df_to_csv(odd_stuff, "test.csv", sep='|')First it creates a list of str objects from the objects in a row. None becomes an empty str and anything else becomes str(obj) wrapped in double quotes. Next I join all the str objects together separated by sep.
Hope this is clear now.