1
shidifen OP 我使用了,__dict__,有没有更好的办法。
|
2
thautwarm 2018-01-09 21:47:07 +08:00
能不能具体说下例子。你是要遍历 parse 的结果?
你使用__dict__也很迷。 综上,missing context. |
3
shidifen OP 比如下面的例子:
import psqlparse query=r"select (b.script_name),'中' from (select * from temp.halfisolateworkjob20171221) a left join (select * from temp.script2table) b on a.schema_name||'.'||a.table_name=b.table_name where a.create_time <'20171201' and a.owner='app_vgop' and schema_name='SESSION' and process_flag=false and b.script_name is not null order by 1" query2=r"insert into dis.td_bd_area_info_d SELECT A.DEAL_DATE,A.INT_ID,A.ZH_LABEL,A.COUNTY_ID, B.ZH_LABEL OUNTY_NAME,B.CITY_ID,case when B.city_id = '40' then '邢台市' when B.city_id = '33' then '秦皇岛市' when B.city_id = '41' then '邯郸市' when B.city_id = '34' then '沧州市' when B.city_id = '36' then '廊坊市' when B.city_id = '32' then '石家庄市' when B.city_id = '37' then '张家口市' when B.city_id = '38' then '保定市' when B.city_id = '42' then '唐山市' when B.city_id = '43' then '衡水市' when B.city_id = '39' then '承德市'ELSE '其他' END ,CASE WHEN A.CELL_SOURCE in ('铁通割接','无线宽带','新国标','自建','自建无线宽带') and A.COVER_TYPE IN ('0','1','2','3') THEN '自建有线' WHEN A.CELL_SOURCE in ('铁通割接','无线宽带','新国标','自建','自建无线宽带') and A.COVER_TYPE IN ('4') THEN '自建无线(WLAN)' WHEN A.CELL_SOURCE in ('铁通割接','无线宽带','新国标','自建','自建无线宽带') and A.COVER_TYPE IN ('6') THEN '自建无线( 4G )' WHEN A.CELL_SOURCE in ('铁通割接','无线宽带','新国标','自建','自建无线宽带') and A.COVER_TYPE IN ('6') THEN '自建无线( 4G )' WHEN A.CELL_SOURCE in ('第三方割接','第三方无线宽带') and A.COVER_TYPE IN ('0','1','2','3') THEN '三方有线' WHEN A.CELL_SOURCE in ('第三方割接','第三方无线宽带') and A.COVER_TYPE IN ('4') THEN '三方无线( WLAN )' else '其他' end,case when A.AREA_TYPE = '市区(含县城)' then '市区' when A.AREA_TYPE = '乡镇(含城乡结合部)' then '乡镇' when A.AREA_TYPE = '农村' then '农村' else '其他' end,A.CELL_SOURCE,A.COVER_TYPE,A.HOUSE_NUM,ROW_NUMBER() OVER (PARTITION BY A.INT_ID ORDER BY A.MODIFY_TIME DESC , B.MODIFY_TIME DESC ) RN FROM DW.TD_RMS_ADD_CELL_D A LEFT JOIN DW.TD_RMS_COUNTY_D B ON A.COUNTY_ID = B.INT_ID AND B.DEAL_DATE = 20170101 where A.DEAL_DATE = 20170101;" statements = psqlparse.parse(query1) used_tables = statements[0] dir(used_tables.from_clause.items[0]) \每一个 sql 的内容不同,返回的值也是不同的,下面这个在简单的 sql 中可以,所以我必须得到 statements 的全部内部,或者是遍历它,否则不知道结构,无法处理。 |
4
shidifen OP sql 不同,返回的结构可以不同。
|
5
thautwarm 2018-01-09 22:26:58 +08:00 1
statements 里每个元素如果是相同类型却又不同 attributes 的话,那不好意思这还真只能__dict__。
如果每个元素.__class__不一样,你倒是可以写一个 def handle_each(obj): if isinstance(obj, node_type1): ... elif ... ... else: ... def handle(statements): for stmt in statements: if isinstance(stmt, some_nested_struct): # 嵌套你就跟进去 handle(stmt) else: handle_each(stmt) 你查一下他这个库有没有自带的 serializer, 有的话应该是直接到 xml/json。 |
6
shidifen OP 好的,我试试看。
|
7
shidifen OP 多谢!!!!
|
8
shidifen OP 已经解决,非常感谢
|