使用pyarrow读取csv文件

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

我正在尝试使用 python 库 pyarrow 读取 csv 文件,但在读取文件时遇到问题,因为对于某些字段,我的值有“\N”(这意味着这是一个空值)。 问题是我在阅读时无法跳过这个值...

这是我的代码:

parse_options = csv.ParseOptions(delimiter=chr(1))
read_options = csv.ReadOptions(column_names=columns)
convert_options = csv.ConvertOptions(column_types=schema_table, include_columns=columns, include_missing_columns=True, null_values=True)

with hdfs.open_input_file("path") as f:
    csv_file = csv.read_csv(f, read_options=read_options, parse_options=parse_options, convert_options=convert_options)

我的错误:

ArrowInvalid: In CSV column #59: CSV conversion error to int64: invalid value '\N'

当我尝试使用分隔符之间没有值的文件时,我没有问题......

非常感谢!

python csv pyarrow
2个回答
0
投票
parse_options = csv.ParseOptions(delimiter=chr(1), null_values=['\\N'])
read_options = csv.ReadOptions(column_names=columns)
convert_options = csv.ConvertOptions(column_types=schema_table, 
include_columns=columns, include_missing_columns=True)

with hdfs.open_input_file("path") as f:
    csv_file = csv.read_csv(f, read_options=read_options, 
parse_options=parse_options, convert_options=convert_options)

0
投票

您所要做的就是通过将“\N”包含在您的convert_options的null_values参数中来指定将其解释为null。

convert_options = csv.ConvertOptions(column_types=schema_table, 
                                 include_columns=columns, 
                                 include_missing_columns=True, 
                                 null_values=['\N'])

希望有帮助。

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