【问题标题】:Passing a list of dictionaries to a function that accepts a json file将字典列表传递给接受 json 文件的函数
【发布时间】:2018-10-10 02:31:38
【问题描述】:

我有一个字典列表,我想传递给一个接受 json 文件的函数。

我目前的方法是 (1) 使用 json.dumps() 将字典列表转换为 json 并 (2) 通过 StringIO() 将其作为参数传递

我得到了错误。 AttributeError: 'str' object has no attribute 'decode'

我不确定如何解决这个问题,或者即使我使用了正确的方法。任何帮助表示赞赏。

编辑:

代码示例

import tdclient as td
import json
from io import StringIO

l = [{'a': 1}, {'b':2}, {'c':3}]
l_json = json.dumps(l)

with td.Client(TD_APIKEY) as con:
    con.import_file('test', 'temp', 'json', StringIO(l_json))

和错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-64-d81f043acd58> in <module>()
      7 
      8 with td.Client(TD_APIKEY) as con:
----> 9     con.import_file('test', 'temp', 'json', StringIO(l_json))

~/anaconda3/lib/python3.6/site-packages/tdclient/client.py in import_file(self, db_name, table_name, format, file, unique_id)
    605         Returns: float represents the elapsed time to import data
    606         """
--> 607         return self.api.import_file(db_name, table_name, format, file, unique_id=unique_id)
    608 
    609     def results(self):

~/anaconda3/lib/python3.6/site-packages/tdclient/import_api.py in import_file(self, db, table, format, file, unique_id, **kwargs)
     61         Returns: float represents the elapsed time to import data
     62         """
---> 63         with contextlib.closing(self._prepare_file(file, format, **kwargs)) as fp:
     64             size = os.fstat(fp.fileno()).st_size
     65             return self.import_data(db, table, "msgpack.gz", fp, size, unique_id=unique_id)

~/anaconda3/lib/python3.6/site-packages/tdclient/api.py in _prepare_file(self, file_like, fmt, **kwargs)
    426             packer = msgpack.Packer()
    427             with contextlib.closing(self._read_file(file_like, fmt, **kwargs)) as items:
--> 428                 for item in items:
    429                     try:
    430                         mp = packer.pack(item)

~/anaconda3/lib/python3.6/site-packages/tdclient/api.py in _read_json_file(self, file_like, **kwargs)
    471         # current impl doesn't torelate any JSON parse error
    472         for s in file_like:
--> 473             record = json.loads(s.decode("utf-8"))
    474             self._validate_record(record)
    475             yield record

AttributeError: 'str' object has no attribute 'decode'

【问题讨论】:

  • 能否向我们展示您的代码和一些示例数据?
  • 解决方案是停止尝试在字符串上调用decode 方法。如果您需要更详细的答案,您必须提供更详细的问题 - minimal reproducible example
  • 抱歉,我添加了一个例子。

标签: python json io


【解决方案1】:

Import_file 期待 BytesIO 而不是 StringIO(请参阅他们的 test

试试这个

import tdclient as td
import json
from io import BytesIO

l = [{'a': 1}, {'b':2}, {'c':3}]
l_json = json.dumps(l)

with td.Client(2) as con:
    con.import_file('test', 'temp', 'json', BytesIO(bytes(l_json, 'utf-8')))

我们正在编码只是让它解码,但它应该可以工作。我没有 API 密钥来进一步验证,但我克服了你提到的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2015-10-02
    • 1970-01-01
    相关资源
    最近更新 更多