Skip to content

ENH: Add excel_sep_hint parameter to to_csv for Excel compatibility #61692

Open
@EwoutH

Description

@EwoutH

Feature Type

  • Adding new functionality to pandas

Problem Description

It would be great if Pandas could generate CSV files that Excel automatically opens with the correct delimiter. When using semicolons or other non-comma separators, Excel often opens CSV files with all data in one column unless a sep= hint is present at the beginning of the file. Currently, users must write custom code to add this hint.

Feature Description

Add a boolean parameter excel_sep_hint to to_csv() that prepends a delimiter hint for Excel:

def to_csv(self, ..., excel_sep_hint=False):
    """
    excel_sep_hint : bool, default False
        If True, prepend 'sep=' line to help Excel detect delimiter
    """

Usage:

df.to_csv('data.csv', sep=';', excel_sep_hint=True)

Output:

sep=;
col1;col2;col3
val1;val2;val3

Alternative Solutions

Users currently write wrapper functions or handle files manually:

with open('file.csv', 'w') as f:
    f.write('sep=;\n')
df.to_csv('file.csv', sep=';', mode='a')

Additional Context

The sep= hint is a standard Excel feature for delimiter detection. This would improve pandas-Excel interoperability, especially useful in European locales where semicolons are common CSV separators.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions