У меня работает. Это может быть код, который предоставляет функцию csv_report()
. Ниже приведена общая функция c, которую вы можете использовать для добавления / создания любого csv из данных словаря:
import csv
def append_csv_dict(path, data):
'''
Append a csv with a dictionary keys as column headers
Args:
path (str): Path to the csv file
data (dict): Dictionary with keys as column headers
and values as column data
'''
with open(path, 'a') as file:
# set the field names to the keys of the dictionary
fieldnames = list(data.keys())
writer = csv.DictWriter(file, fieldnames=fieldnames)
# write the header if the file is new
if file.tell() == 0:
writer.writeheader()
# write the row
writer.writerow(data)
# write some of the names to the file
append_csv_dict('mycsv.csv', {'email': 'michael.lawson@reqres.in', 'message': 'Editor role has been enabled'})
append_csv_dict('mycsv.csv', {'email': 'lidsay.ferguson@reqres.in', 'message': 'Person already has editor access'})
append_csv_dict('mycsv.csv', {'email': 'tobias.funke@reqres.in', 'message': 'Editor role has been enabled'})
append_csv_dict('mycsv.csv', {'email': 'byron.fields@reqres.in', 'message': 'Person Account Created and ...'})
append_csv_dict('mycsv.csv', {'email': 'george.edwards@reqres.in', 'message': 'Person Account Created and ...'})
append_csv_dict('mycsv.csv', {'email': 'rachel.howell@reqres.in', 'message': 'Person Account Created and ...'})
Вывод:
email,message
michael.lawson@reqres.in,Editor role has been enabled
lidsay.ferguson@reqres.in,Person already has editor access
tobias.funke@reqres.in,Editor role has been enabled
byron.fields@reqres.in,Person Account Created and ...
george.edwards@reqres.in,Person Account Created and ...
rachel.howell@reqres.in,Person Account Created and ...