V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
shidifen
V2EX  ›  Python

如何遍历一个复杂的对象结构

  •  
  •   shidifen · 2018-01-09 21:37:25 +08:00 · 2401 次点击
    这是一个创建于 2270 天前的主题,其中的信息可能已经有所发展或是发生改变。
    源码的名字是:psqlparse,是一个基于 libpg_query 来解析 sql 的,其中使用了 cython,
    地址是 https://github.com/alculquicondor/psqlparse,给个解决思路就行,这个程序不复杂,大约能看懂,已经看了一天,问题是:复杂的对象结构,其中包含 json,list 的,还是嵌套的,如何遍历它,优雅的。
    8 条回复    2018-01-10 21:54:23 +08:00
    shidifen
        1
    shidifen  
    OP
       2018-01-09 21:37:59 +08:00
    我使用了,__dict__,有没有更好的办法。
    thautwarm
        2
    thautwarm  
       2018-01-09 21:47:07 +08:00
    能不能具体说下例子。你是要遍历 parse 的结果?
    你使用__dict__也很迷。
    综上,missing context.
    shidifen
        3
    shidifen  
    OP
       2018-01-09 22:12:50 +08:00
    比如下面的例子:
    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 的全部内部,或者是遍历它,否则不知道结构,无法处理。
    shidifen
        4
    shidifen  
    OP
       2018-01-09 22:13:38 +08:00
    sql 不同,返回的结构可以不同。
    thautwarm
        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。
    shidifen
        6
    shidifen  
    OP
       2018-01-09 22:52:30 +08:00
    好的,我试试看。
    shidifen
        7
    shidifen  
    OP
       2018-01-09 22:52:49 +08:00
    多谢!!!!
    shidifen
        8
    shidifen  
    OP
       2018-01-10 21:54:23 +08:00
    已经解决,非常感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1405 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:44 · PVG 07:44 · LAX 16:44 · JFK 19:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.