[Python] How to handle inconsistent columns (ragged rows) in a delimited file using Polars?

Discussão em 'Python' iniciado por Stack, Janeiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am working with a legacy system that generates delimited files (e.g., CSV), but the number of columns in these files is inconsistent across rows (ragged rows). I am reading the file from ADLS with Polars, but I'm encountering an issue depending on the structure of the second row in the file.

    pl.read_csv('sample.csv', has_header=False, skip_rows=1, infer_schema=False, infer_schema_length=None, ignore_errors=True)

    • If the second row has more columns than subsequent rows, Polars reads the file successfully and fills the missing values in subsequent rows with null.
    • However, if subsequent rows have more columns than the second row, I get the following exception

    ComputeError: found more fields than defined in 'Schema' Consider setting 'truncate_ragged_lines=True'.

    Is there a way to handle such cases dynamically in Polars, or do I need to preprocess the file to fix these inconsistencies before reading? Any alternative approaches or solutions to this problem would be appreciated!

    Example Data - Failure

    ID,Name,Age
    1,John,28
    2,Jane,35,California,USA
    3,Emily,22
    4,Michael,40,Australia,Melbourne


    Example Data - Success

    ID,Name,Age
    2,Jane,35,California,USA
    1,John,28
    3,Emily,22
    4,Michael,40,Australia,Melbourne

    Continue reading...

Compartilhe esta Página