Web2py将CSV文件作为服务的参数传递

问题描述 投票:0回答:1

目前,我正在为web2py编写Web服务。我想将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

这里是我的Web服务代码:

@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)

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

我期待您的回答。

关于法比安

json python-3.x csv web-services web2py
1个回答
0
投票

gluon.contrib.simplejsonrpc中有一个错误,此后已修复。只需升级到2.18.5之后的任何版本的web2py,它就可以正常工作(相反,尽管有问题的代码实际上在JSONRPCError类中,这表明您的代码还有其他部分,您不会再遇到该特定错误。引发了异常)。

© www.soinside.com 2019 - 2024. All rights reserved.