> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Error: need to escape, but no escapechar set - Pandas
- URL: https://datascientyst.com/error-need-to-escape-but-no-escapechar-set-pandas/
- Published: 2023-03-18T22:17:54.000Z
- Updated: 2023-03-18T22:26:20.000Z
- Author: John D K
- Tags: to_csv()

In this tutorial, we'll see how to solve a common Pandas and Python error – "Error: need to escape, but no escapechar set". We get this error from Pandas when we try to save DataFrame as a CSV file.

Let's see several examples of how to reproduce and solve this error.

## Pandas - Error: need to escape, but no escapechar set

When we try to use `to_csv` we may get error in Pandas:

> Error: need to escape, but no escapechar set

The example below demonstrate the error:

```python
import pandas
import csv

data = {'A': ['1.0627', '0625', '"LTE,eHRPD"'],
   	 'B': ['0.3', '', '"ZTE\A"'],
  	 }
df = pandas.DataFrame(data)
display(df)

print(df.to_csv(quoting=csv.QUOTE_NONE))

```

The error is caused by using `csv.QUOTE_NONE`. Let's check two different solutions to this error.

## Pandas to\_csv escapechar

First way to solve the error is by using `escapechar='\\'`:

```python
print(df.to_csv(quoting=csv.QUOTE_NONE, quotechar='',escapechar='\\'))

```

This will produce CSV file as:

```
,A,B
0,1.0627,0.3
1,0625,
2,"LTE\,eHRPD","ZTE\\A"

```

## Solution - QUOTE\_ALL and QUOTE\_MINIMAL

Alternatively we can solve the error by setting parameters:

- `quoting=csv.QUOTE_MINIMAL` \- to only quote fields that contain special characters.
- `quoting=csv.QUOTE_ALL` \- all fields will be quoted

### QUOTE\_MINIMAL

We can see the output of:

```python
df.to_csv(quoting=csv.QUOTE_MINIMAL)

```

the will be quoting the values in row 2:

```
,A,B
0,1.0627,0.3
1,0625,
2,"""LTE,eHRPD""","""ZTE\A"""

```

### QUOTE\_ALL

We can see the output of - `QUOTE_ALL`:

```python
df.to_csv(quoting=csv.QUOTE_ALL)

```

then all values will be quoted:

```
"","A","B"
"0","1.0627","0.3"
"1","0625",""
"2","""LTE,eHRPD""","""ZTE\A"""

```

![](https://datascientyst.com/content/images/2023/03/error-need-to-escape-but-no-escapechar-set-pandas.webp)

## Python - Error: need to escape, but no escapechar set

The code below shows how to reproduce Python error - "Error: need to escape, but no escapechar set"

### QUOTE\_MINIMAL

```python
import csv

data = [["John", "Doe", "john@example.com"], ["Jane", "Doe", "jane@example.com", "Hello, \"world\"!"]]

with open("output.csv", "w", newline="") as f:
    writer = csv.writer(f, quoting=csv.QUOTE_NONE)
    for row in data:
   	 writer.writerow(row)

```

result:

```
Error: need to escape, but no escapechar set

```

So the error is solved by:

```python
writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)

```

### Filter quoted values

As alternative solution we can filter the values by list comprehension:

```python
import csv

data = [["John", "", "Doe"], ["Jane", " ", "Doe"]]

with open("output.csv", "w", newline="") as f:
    writer = csv.writer(f)
    for row in data:
   	 row = [s if s else " " for s in row]  # filter problematic strings
   	 writer.writerow(row)

```

## Summary

To summarize, in this article, we've seen how to solve Python and Pandas error:

> Error: need to escape, but no escapechar set

And finally you can find useful resources which will help you to solve Pandas `to_csv` output quoting issues.

## Resources

- [\_csv.Error: need to escape, but no escapechar set](https://github.com/pandas-dev/pandas/issues/16298?ref=datascientyst.com)
- [to\_csv](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to%5Fcsv.html?ref=datascientyst.com)
- [Python CSV File Reading and Writing](https://docs.python.org/3/library/csv.html?ref=datascientyst.com)