Skip to content

escapechar=',' Causes Double Commas in Output (from pandas) #123109

Closed as not planned
@wooseogchoi

Description

@wooseogchoi

Bug report

Bug description:

This is related to the issue in pandas.

pandas-dev/pandas#59454

The pandas relies on the writer in cpython's csv to save DataFrame to the given csv file.
When the escapechar is given to pandas, it passes it to writer object as escapedchar.
The quoting is set to the default value, csv.QUOTE_MINIMAL.
The expectation is that data surrounded the given escaped char or delimiter are surronded by quotes. Instead, csv writer just added escaped char after escapted char.
Please see the example below.

# Add a code block here, if required
import csv

data = [
    ['Name', 'Branch', 'Year', 'CGPA'],
    ['Nik,hil', 'COE', 2, 9.0],
    ['San,chit', 'COE', 2, 9.1],
    ['Adi,tya', 'IT', 2, 9.3],
    ['Sa,gar', 'SE', 1, 9.5],
    ['Pra,teek', 'MCE', 3, 7.8],
    ['Sa,hil', 'EP', 2, 9.1]
]

with open('university_records.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, escapechar=',')
    writer.writerows(data)

Name,Branch,Year,CGPA
Nik,,hil,COE,2,9.0
San,,chit,COE,2,9.1
Adi,,tya,IT,2,9.3
Sa,,gar,SE,1,9.5
Pra,,teek,MCE,3,7.8
Sa,,hil,EP,2,9.1

# Add a code block here, if required
import csv

data = [
    ['Name', 'Branch', 'Year', 'CGPA'],
    ['Nik,hil', 'COE', 2, 9.0],
    ['San,chit', 'COE', 2, 9.1],
    ['Adi,tya', 'IT', 2, 9.3],
    ['Sa,gar', 'SE', 1, 9.5],
    ['Pra,teek', 'MCE', 3, 7.8],
    ['Sa,hil', 'EP', 2, 9.1]
]

with open('university_records.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, escapechar='.')
    writer.writerows(data)

Name,Branch,Year,CGPA
"Nik,hil",COE,2,9..0
"San,chit",COE,2,9..1
"Adi,tya",IT,2,9..3
"Sa,gar",SE,1,9..5
"Pra,teek",MCE,3,7..8
"Sa,hil",EP,2,9..1

CPython versions tested on:

3.11

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions