我有一个如下所示的输入字符串。我想根据逗号将其解析为如下所示的输出。问题是,有时像下面的示例一样,括号内包含逗号,引号内也包含引号。我对regexpression匹配不是很在行,所以任何提示都非常感谢。
"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"
输出:
{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'}
尝试:
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
输出:
['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']