You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "Afsalmc (via GitHub)" <gi...@apache.org> on 2024/07/07 09:16:22 UTC
[I] Download to CSV fails when values contain null characters [superset]
Afsalmc opened a new issue, #29508:
URL: https://github.com/apache/superset/issues/29508
### Bug description
If the Query result contains null characters Exporting to CSV fails. Getting error logs:
_csv.Error: need to escape, but no escapechar set
### How to reproduce the bug
1. Go to SQL Lab
2. Execute query that can produce result like '\x00\x00\x00\x00v\x1e\x04\x0c'
3. Download to CSV
### Screenshots/recordings
_No response_
### Superset version
4.0.1
### Python version
3.9
### Node version
16
### Browser
Chrome
### Additional context
_No response_
### Checklist
- [X] I have searched Superset docs and Slack and didn't find a solution to my problem.
- [X] I have searched the GitHub issue tracker and didn't find a similar bug report.
- [X] I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Download to CSV fails when values contain null characters [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
dosubot[bot] commented on issue #29508:
URL: https://github.com/apache/superset/issues/29508#issuecomment-2212382449
<!-- Greeting -->
Hey @Afsalmc! 👋 I'm here to help you with any bugs, questions, or contributions you have while waiting for a human maintainer. Let's squash those bugs together!
<!-- Answer -->
To resolve the issue where downloading query results to CSV fails with the error `_csv.Error: need to escape, but no escapechar set` when the values contain null characters, you need to set the `escapechar` parameter in the `CSVReader` configuration.
Here is an example of how you can modify the `CSVReader` configuration to include the `escapechar`:
```python
UploadCommand(
upload_database.id,
CSV_UPLOAD_TABLE,
create_csv_file(CSV_FILE_WITH_NULLS),
None,
CSVReader({"null_values": ["N/A", "None"], "escapechar": "\\"}),
).run()
```
This sets the escape character to a backslash (`\`), which will handle the null characters properly during the CSV export.
Additionally, you can use the `df_to_escaped_csv` function from the `superset/utils/csv.py` module to ensure that special characters are properly escaped in your DataFrame before exporting it to CSV:
```python
import pandas as pd
from superset.utils.csv import df_to_escaped_csv
# Assuming df is your DataFrame
csv_data = df_to_escaped_csv(df)
with open('output.csv', 'w') as file:
file.write(csv_data)
```
This function will escape problematic characters and set the escape character to a backslash (`\`), ensuring that the CSV export handles null characters and other special cases correctly <sup>[[1]](https://github.com/apache/superset/blob/master/tests/integration_tests/databases/commands/upload_test.py)</sup><sup>[[2]](https://github.com/apache/superset/blob/master/superset/utils/csv.py)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Download to CSV fails when values contain null characters [superset]
Posted by "Afsalmc (via GitHub)" <gi...@apache.org>.
Afsalmc closed issue #29508: Download to CSV fails when values contain null characters
URL: https://github.com/apache/superset/issues/29508
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Download to CSV fails when values contain null characters [superset]
Posted by "Afsalmc (via GitHub)" <gi...@apache.org>.
Afsalmc commented on issue #29508:
URL: https://github.com/apache/superset/issues/29508#issuecomment-2212383110
**Solution:**
We can control what escape characters should be set while exporting pandas Dataframe through a superset config. By default we are not setting any escape character, only the encoding to use is set.
`CSV_EXPORT = {"encoding": "utf-8"}`
We can modify this config to specify an escape character,
`CSV_EXPORT = {"encoding": "utf-8","escapechar": "\\"}`
which will then passed as **kwargs to to_csv() in **csv.py**
```
def df_to_escaped_csv(df: pd.DataFrame, **kwargs: Any) -> Any:
...........................................................
...........................................................
return df.to_csv(**kwargs)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org