Skip to content

Instantly share code, notes, and snippets.

@WritingPanda
Last active August 15, 2024 21:16
Show Gist options
  • Save WritingPanda/8e4ebfeb3fc4cc48a13275a1d468c428 to your computer and use it in GitHub Desktop.
Save WritingPanda/8e4ebfeb3fc4cc48a13275a1d468c428 to your computer and use it in GitHub Desktop.
Change a YAML file to a CSV file
# Change https://github.com/github/docs/blob/main/src/secret-scanning/data/public-docs.yml to a CSV
import yaml
import csv
from datetime import datetime
def load_yaml_file(filename: str) -> list:
with open(filename, "r") as stream:
try:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return data
def convert_yaml_to_csv(yaml_data: list, csv_filename: str) -> None:
with open(csv_filename, mode="w") as file:
fieldnames = ["provider", "supportedSecret", "secretType", "hasPushProtection"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for data in yaml_data:
writer.writerow(
{
"provider": data["provider"],
"supportedSecret": data["supportedSecret"],
"secretType": data["secretType"],
"hasPushProtection": data["hasPushProtection"],
}
)
if __name__ == "__main__":
# Expected that the file is in the root of the project
yaml_filename = "secrets.yml"
data = load_yaml_file(yaml_filename)
# Adding so we have some difference between multiple runs for testing
time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
csv_filename = f"{yaml_filename}-{time}.csv"
# Run the function
convert_yaml_to_csv(data, csv_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment