Untitled

This Python script processes a collection of YAML files from a specified directory, extracts specific info (date, winner, MVP), and writes the data to a CSV file. Each processed file updates the console with its status, indicating which file is currently being processed.
 avatar
unknown
python
10 months ago
826 B
3
Indexable
import yaml
import csv
import glob

yaml_files = glob.glob('./ipl/*.yaml')

rows=[]

for i,each_file in enumerate(yaml_files):
    print("Processing file {} of {} file name: {}".format(i+1,len(yaml_files),each_file))

    with open(each_file) as f:
        data = yaml.safe_load(f)
        values=dict()
        values["date"]=data["info"]['dates']
        values["winner"]=data["info"]["outcome"].get("winner", "Eliminator match - no winner")
        values["mvp"]=data["info"].get("player_of_match", ['N/A'])[0]
    rows.append([values["date"],values["winner"],values["mvp"]])

with open('output_csv_file.csv', 'w', newline='') as out:
    csv_writer = csv.writer(out)
    csv_writer.writerow(["date","winner","mvp"])
    csv_writer.writerows(rows)
    print("Output file output_csv_file.csv created")
Editor is loading...
Leave a Comment