首页 > 解决方案 > Web2py 将 CSV 文件作为服务参数传递

问题描述

我现在正在为 web2py 编写一个网络服务。我想将 CSV 文件作为 Web 服务的参数传递并读取 Web 服务中的 CSV。我的问题是,当我尝试传递 CSV 时,出现错误:

<class 'NameError'> name 'basestring' is not defined
Version
web2py™     Version 2.18.5-stable+timestamp.2019.04.08.04.22.03
Python  Python 3.6.8: /bin/python3 (prefix: /usr)
Traceback   

Traceback (most recent call last):
  File "/web2py/gluon/restricted.py", line 219, in restricted
    exec(ccode, environment)
  File "/web2py/applications/testwebservice/controllers/default.py", line 83, in <module>
  File "/home/ff/web2py/gluon/globals.py", line 421, in <lambda>
    self._caller = lambda f: f()
  File "/web2py/applications/testwebservice/controllers/default.py", line 20, in index
    schaar = webservice.schaar(csv_reader_object)
  File "/web2py/gluon/contrib/simplejsonrpc.py", line 112, in <lambda>
    return lambda *args, **vars: self.call(attr, *args, **vars)
  File "/web2py/gluon/contrib/simplejsonrpc.py", line 144, in call
    self.error.get('data', None))
  File "/web2py/gluon/contrib/simplejsonrpc.py", line 38, in __init__
    if isinstance(data, basestring):
NameError: name 'basestring' is not defined

这是我的网络服务代码:

@service.json
@service.jsonrpc
@service.jsonrpc2
def schaar(csv_reader_object):

    csvlist = csv.reader(csv_reader_object, delimiter=',')

    csv= list()
    headers = csvlist[0]

    for i,e in enumerate(csvlist):
        if i == 0:
            continue

        row_dict=dict()
        for i2, e2 in enumerate(e):
            row_dict[headers[i2]] = e2 if (e2 != None or e2 != "") else None

        csv.append(row_dict)

    return (csv)

希望您能够帮助我。我已经尝试将二进制文件转换为字符串,但它以某种方式失败了。我已经尝试过BytesIOandStringIO了。

我期待着您的回答。

问候费边

标签: jsonpython-3.xcsvweb-servicesweb2py

解决方案


中存在一个错误gluon.contrib.simplejsonrpc,该错误已得到修复。只需在 2.18.5 之后升级到 web2py 的任何版本,它应该可以工作(相反,你不会再遇到那个特定的错误,尽管有问题的代码实际上是在JSONRPCError类中,表明你的代码的其他部分已经引发了例外)。


推荐阅读