Answer the question
In order to leave comments, you need to log in
How to convert dictionary to csv/exel?
Good day to all.
There is a variable in which I receive the list of dictionaries. I also write json these dictionaries.
Actually json itself https://pastebin.com/DvbaMgRr
How can I write this list of dictionaries to csv/exel?
I have already tried a lot of things ... I suffered with the encoding and separators (for some reason, it swears on the delimeter).
I'm trying to implement this with this function:
def write_csv(dannie, imyafayla, encoding = "utf-8"):
with open(imyafayla, "w", newline="", encoding=encoding) as f1:
fieldnames = ["id", "date", "linuxe_date", "text", "likes", "comments", "reposts", "views", "max_photos"]
writer = csv.DictWriter(f1, fieldnames = fieldnames)
writer.writeheader()
writer.writerows(dannie)
print("Все записалось", imyafayla)
f1.close()
write_csv(filtered_data, "faylik.csv", encoding="utf-8")
Answer the question
In order to leave comments, you need to log in
import csv
import json
def write_csv(data, file, encoding="utf-8"):
with open(file, "w", newline="", encoding=encoding) as f1:
fieldnames = data[0].keys()
writer = csv.DictWriter(f1, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
with open('type_info.json', 'r') as f:
data = json.load(f)
write_csv(data, "out.csv", encoding="utf-8")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question