I have an input string like the one shown below. I would like to parse it based on commas into a dict like the output shown below. The problem is that sometimes there are commas contained inside of parenthesis like the example below, and also quotes inside quotes. I'm not that handy with regexpression matching, so any tips are greatly appreciated.
input:
"ty_event_name, from_unixtime(unix_timestamp(regexp_replace(ty_date,'/','-'),'MM-dd-yyyy'),'yyyy-MM-dd') as ty_date,'${hiveconf:run_dt}' as sessions_fy,orders_xy"
output:
{1:'ty_event_name',
2:'from_unixtime(unix_timestamp(regexp_replace(ty_date,'/','-'),'MM-dd-yyyy'),'yyyy-MM-dd') as ty_date',
3:''${hiveconf:run_dt}' as sessions_fy',
4:'orders_xy'}
Tried:
import pandas as pd
import numpy as np
import re
teststr="ty_event_name, from_unixtime(unix_timestamp(regexp_replace(ty_date,'/','-'),'MM-dd-yyyy'),'yyyy-MM-dd') as ty_date,'${hiveconf:run_dt}' as sessions_fy,orders_xy"
tstr=re.sub('(?!\B"[^"]*),(?![^"]*"\B)',',',teststr).split()
tstr
Output:
['ty_event_name,',
"from_unixtime(unix_timestamp(regexp_replace(ty_date,'/','-'),'MM-dd-yyyy'),'yyyy-MM-dd')",
'as',
"ty_date,'${hiveconf:run_dt}'",
'as',
'sessions_fy,orders_xy']