Use Multiple Character Delimiter in Python Pandas read_csv

后端 未结 4 1231
小蘑菇
小蘑菇 2020-11-28 15:59

It appears that the pandas read_csv function only allows single character delimiters/separators. Is there some way to allow for a string of characters to be

4条回答
  •  无人及你
    2020-11-28 16:15

    As Padraic Cunningham writes in the comment above, it's unclear why you want this. The Wiki entry for the CSV Spec states about delimiters:

    ... separated by delimiters (typically a single reserved character such as comma, semicolon, or tab; sometimes the delimiter may include optional spaces),

    It's unsurprising, that both the csv module and pandas don't support what you're asking.

    However, if you really want to do so, you're pretty much down to using Python's string manipulations. The following example shows how to turn the dataframe to a "csv" with $$ separating lines, and %% separating columns.

    '$$'.join('%%'.join(str(r) for r in rec) for rec in df.to_records())
    

    Of course, you don't have to turn it into a string like this prior to writing it into a file.

提交回复
热议问题