Python:TypeError:预期字节,发现(尝试做嵌套元数据)

发布于 2025-01-31 21:00:06 字数 529 浏览 3 评论 0原文

我想将嵌套的元数据如下:

schema = pa.schema([pa.field('Id', pa.string(), metadata={"pg_dtype": {"dtype": "int[36]", "pkey": "1"}}), 
                    pa.field('Name', pa.string(), metadata={"pg_dtype": {"dtype": "varchar[20]", "pkey": "1"}})])

但是获得此错误,

TypeError: expected bytes, dict found

我指的是这个嵌套的示例以制作嵌套的元数据

people = {'type': {'name': 'John', 'age': '27', 'sex': 'Male'},
          'type': {'name': 'Marie', 'age': '22', 'sex': 'Female'}}

I want to make nested metadata as below:

schema = pa.schema([pa.field('Id', pa.string(), metadata={"pg_dtype": {"dtype": "int[36]", "pkey": "1"}}), 
                    pa.field('Name', pa.string(), metadata={"pg_dtype": {"dtype": "varchar[20]", "pkey": "1"}})])

but getting this error

TypeError: expected bytes, dict found

I refer to this nested dict example to make nested metadata

people = {'type': {'name': 'John', 'age': '27', 'sex': 'Male'},
          'type': {'name': 'Marie', 'age': '22', 'sex': 'Female'}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2025-02-07 21:00:06

metadata应为dict带有键和值的 str (或 bytes )。您不能嵌套尺寸。您必须将嵌套的dict(pg_dtypes)倾倒为JSON字符串:

import pyarrow as pa
import json

schema = pa.schema([
    pa.field('Id', pa.string(), metadata={"pg_dtype": json.dumps({"dtype": "int[36]", "pkey": "1"})}), 
    pa.field('Name', pa.string(), metadata={"pg_type": json.dumps({"dtype": "varchar[20]", "pkey": "1"})})
])

schema.field('Name').metadata
>>> {b'pg_type': b'{"dtype": "varchar[20]", "pkey": "1"}'}

这有效,但我不确定这是PG拾取它​​的正确格式。

metadata should be a dict with keys and values as str (or bytes). You can't have a nested dict in it. You'd have to dump the nested dict (pg_dtypes) as a json string:

import pyarrow as pa
import json

schema = pa.schema([
    pa.field('Id', pa.string(), metadata={"pg_dtype": json.dumps({"dtype": "int[36]", "pkey": "1"})}), 
    pa.field('Name', pa.string(), metadata={"pg_type": json.dumps({"dtype": "varchar[20]", "pkey": "1"})})
])

schema.field('Name').metadata
>>> {b'pg_type': b'{"dtype": "varchar[20]", "pkey": "1"}'}

This works but I'm not sure it's the right format of pg to pick it up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文