【问题标题】:getting pyarrow.lib.ArrowInvalid: CSV parse error: Expected 9 columns, got 1获取 pyarrow.lib.ArrowInvalid:CSV 解析错误:预期 9 列,得到 1
【发布时间】:2021-01-02 14:17:24
【问题描述】:

所以我第一次尝试使用 apache arrow 并想将整个 txt 文件目录读入 pyarrow 数据结构。我得到了


pyarrow.lib.ArrowInvalid: CSV parse error: Expected 9 columns, got 1

当我运行下面的代码时?不知道如何调试它。任何帮助表示赞赏。如果有一本涵盖 python 和 pyarrow 的书也很乐意阅读。


import pyarrow.csv as csv

import pyarrow as pa  


l_all_files = ['x08.txt', 'x21.txt', 'x108.txt']

read_options = csv.ReadOptions( column_names= ('Sol' , 'H20', 'H50', 'H100',  'Date', 'Cv', 'Dys', 'Ple' , 'Cl') ,  skip_rows=26)


df_arrow_all = pa.concat_tables(( csv.read_csv(current_filename,read_options=read_options, ) for current_filename in l_all_files))  

skip_rows = 26 完成后,这条线看起来像这样


$sss        3431    3232    3249  25523  35.66    57  56le   77.73

【问题讨论】:

  • csv 文件的格式是否正确?第一行有逗号吗?是否引用了整个标题行?
  • 嗨@Mike67 更新了问题以显示示例行。我认为 csv.read_csv 足够聪明,可以处理空格而不是逗号。我还认为通过提供列名,文件中就不需要标题了。
  • 数据中没有逗号。它可能被读作单列。试试df_arrow_all = pa.concat_tables(( csv.read_csv(current_filename,read_options=read_options,parse_options=csv.ParseOptions(delimiter='\t' ) for current_filename in l_all_files))
  • 我同意将其作为单列阅读。使用 delim_whitespace=True 对它运行 pandas 可以正常工作,似乎无法弄清楚如何在 pyarrow 中做同样的事情。
  • 我没有看到 pyarrow 的白色选项:arrow.apache.org/docs/python/generated/…。您可能需要使用 csv 模块加载和重新保存数据,以便文件包含逗号。

标签: csv pyarrow


【解决方案1】:
delmt = '\t' 
df = csv.read_csv(current_filename
                 ,read_options=read_options
                 ,parse_options=csv.ParseOptions(delimiter=delmt)

这对我有用! 信用:@Mike67

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 2013-01-21
    • 2021-03-25
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    相关资源
    最近更新 更多