首页 > 解决方案 > 如何使用 Python 将 Json 行转换为镶木地板?

问题描述

我需要用 Python 以一种简单的方式来完成它。我正在尝试使用 Pandas,但我才刚刚开始,这对我来说非常困难。

现在我正在尝试使用 json2parquet:

try:
    input_filename= '/tmp/source_file'
    source_file = s3.get_object(Bucket="myBucket", Key="myJsonLinesFile")
    datajson = source_file['Body'].read()
    with open(input_filename, 'wb') as f:
         f.write(datajson)
    convert_json(input_filename, '/tmp/final.parquet')


except Exception as e:
    print(e)   
    raise e

但我得到以下错误:“errorMessage”:“不能混合列表和非列表,非空值”,“errorType”:“ArrowInvalid”,

标签: pythonjsonpandasparquet

解决方案


如果您使用的是 pandas 0.25.3 版本,您可以安装 fastparquet 或 pyarrow 库并执行以下代码

>>> df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
>>> df.to_parquet('df.parquet.gzip',
...               compression='gzip')  # doctest: +SKIP

更多细节可以在这里找到 - https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_parquet.html

以下是链接

  1. 快速拼花 - https://pypi.org/project/fastparquet/
  2. pyarrow - https://arrow.apache.org/docs/python/install.html#using-pip

推荐阅读