I am trying to convert yaml to json. BUT, Before converting it I need to check if incoming file is yaml or not(this check is mandatory)
I found some piece of code here in Is there a way to determine whether a file is in YAML or JSON format? and found below:
import re
from pathlib import Path
commas = re.compile(r',(?=(?![\"]*[\s\w\?\.\"\!\-\_]*,))(?=(?![^\[]*\]))')
"""
Find all commas which are standalone
- not between quotes - comments, answers
- not between brackets - lists
"""
file_path = Path("example_file.cfg")
signs = commas.findall(file_path.open('r').read())
return "json" if len(signs) > 0 else "yaml"
but my input file is not like :
example_file.cfg
My input would be either example.yaml
or example.json
So I need such comparison without example_file.cfg
Thankful if anything found helpful.