Created
August 4, 2021 21:28
-
-
Save squarepegsys/54c06e8ed1f730b5d515f248bbf9a768 to your computer and use it in GitHub Desktop.
Generic pydemic object to CSV export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# assumptions: | |
# mappings is a list of pydentic objects extending BaseModel - https://pydantic-docs.helpmanual.io | |
# filename is the filename you want to export to | |
import csv | |
from pathlib import Path | |
def out_mapping(filename: str, mappings: List): | |
out_csv = Path(filename) | |
fields = list(mappings[0].__fields_set__) | |
with out_csv.open("w") as out_fp: | |
writer = csv.DictWriter(out_fp, fieldnames=fields) | |
[writer.writerow(x.dict()) for x in mappings] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment