python - 如何获取导致 dask.read_csv 错误的 csv 文件的名称?

标签 python csv error-handling dask

我的目标是并行读取许多 (500+) 个包含测量数据的 csv 文件。为此,我将路径列表 (source_files) 传递给同步客户端。此外,我还指定了数据类型和列名 (order_list)。

df = dd.read_csv(source_files, 
                         names = order_list,
                         include_path_column = True,
                         delimiter = ';',
                         decimal = '.',
                         dtype = dtype,
                         na_values = '.',
                         assume_missing = True,
                         error_bad_lines = False
                         )

df = CLIENT.compute(df).result()

对于损坏的行,我收到以下错误消息:

File "pandas\_libs\parsers.pyx", line 1164, in pandas._libs.parsers.TextReader._convert_tokens

ValueError: cannot safely convert passed user dtype of bool for float64 dtyped data in column 116

在极少数情况下,数据记录器会搞乱写入日志文件,导致 float 出现在我期望 bool 值的位置。我确信我传递给 read_csv 的 dtypes 是正确的,并且可以在绝大多数 csv 文件中得到满足。

有没有办法确定哪个 csv 文件实际导致了错误?知道给定 csv 文件的哪一行导致了异常也很好。

提前致谢!

最佳答案

捕获异常:

代替

df = dd.read_csv(source_files, 
                         names = order_list,
                         include_path_column = True,
                         delimiter = ';',
                         decimal = '.',
                         dtype = dtype,
                         na_values = '.',
                         assume_missing = True,
                         error_bad_lines = False
                         )

df = CLIENT.compute(df).result()

遍历所有并捕获异常

for source_file in source_files:
    try:
        df = dd.read_csv(source_file, 
                             names = order_list,
                             include_path_column = True,
                             delimiter = ';',
                             decimal = '.',
                             dtype = dtype,
                             na_values = '.',
                             assume_missing = True,
                             error_bad_lines = False
                             ) df = dd.read_csv(source_files, 
                             names = order_list,
                             include_path_column = True,
                             delimiter = ';',
                             decimal = '.',
                             dtype = dtype,
                             na_values = '.',
                             assume_missing = True,
                             error_bad_lines = False
                             )
    except ValueError:
        raise Exception('Could not read {}'.format(source_file))

这会告诉您哪个文件失败了,您可以检查原因。 如果他们没有失败,那么只需加入 dfs,你就会进入一个大的,你就完成了。

关于python - 如何获取导致 dask.read_csv 错误的 csv 文件的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55259773/

相关文章:

python - 属性错误: 'module' object has no attribute 'cairo_font_map_get_default'

python - 格式化嵌套的 json 以用于 Python 请求

powershell - 如何导入CSV文件中每行的前两个值电源外壳

tcp - 在 golang 中,TCPConn.Write/Read 会返回什么错误?

error-handling - 如何对 symfony4 中的 api 请求应用验证

python - 如何按键查找特定的 JSON 值?

Python:由于 OSError 无法安装软件包:[Errno 2] 没有这样的文件或目录

postgresql - 使用 psql\copy 导入带有时间戳列 (dd.mm.yyyy hh.mm.ss) 的 .csv

java - 从 Java 对象生成 CSV 并移动到 Azure 存储,无需中间位置

r - 在R中使用错误处理运行多个脚本