【问题标题】:Keep getting type error python不断收到类型错误python
【发布时间】:2016-01-30 21:37:34
【问题描述】:

我正在使用 jqGrid 进行搜索查询。我的参数如下:

function:GL
_search:true
nd:1446209493437
rows:50
page:1
sidx:datetime
sord:asc
filters:{"groupOp":"AND","rules":[{"field":"username","op":"eq","data":"user"},{"field":"description","op":"nc","data":"utility"}]}
searchField:
searchString:
searchOper:

当我使用这些值在本地运行脚本时,它可以正常工作,但是当我使用网页和 cgi 时:

C:\wamp\www\ISaidItBest\assets\cgi-bin\app\Log.py in getAllLogs(self=<app.Log.Log object>)
     91                 elif filters:   # filter options
     92                     buildwhere = ""
=>   93                     rules = filters['rules']
     94                     for idx in range(len(rules)):
     95                         field = rules[idx]['field']
rules undefined, filters = '{"groupOp":"AND","rules":[{"field":"username","o...ield":"description","op":"nc","data":"utility"}]}'
TypeError: string indices must be integers 
      args = ('string indices must be integers',) 
      with_traceback = <built-in method with_traceback of TypeError object>

【问题讨论】:

    标签: python string dictionary integer indices


    【解决方案1】:

    您需要将filtersstr 转换为dict。阅读:https://docs.python.org/2/library/json.html#json.loads

    尝试:

    ....
    buildwhere = ""
    if isinstance(filters, str):
        filters = json.loads(filters)
    ....
    

    【讨论】:

    • 在过滤器上使用 type() 并得到
    • 当你在本地运行脚本时,过滤器是dict,当你作为cgi运行时,过滤器类型是str。
    • 效果很好!谢谢你。我没有意识到 cgi 字段存储被评估为字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多