-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: CSV Export Creates Corrupted Files #6
Comments
QA: data = [["John, Doe", "30", "New York"], ["Jane Doe", "25", "Los Angeles"]]
export_to_csv(data) This produces:
When opened in Excel, the first row appears misaligned because the name “John, Doe” isn’t enclosed in quotes. Can we address this issue by adhering to CSV standards? |
Developer:
I’ll refactor the function to:
Working on the fix now. |
Developer:
Updated code snippet: import csv
def export_to_csv(data, file_name='data.csv'):
try:
with open(file_name, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file, quoting=csv.QUOTE_ALL)
writer.writerows(data)
print(f"File exported successfully: {file_name}")
except Exception as e:
raise ValueError(f"Failed to export CSV: {e}") The changes are in branch fix/csv-export-bug. Please validate and let me know if further adjustments are needed. |
QA:
Everything looks good. This fix resolves the issue completely. Great work! |
Developer: |
Description:
The export_to_csv() function generates corrupted CSV files that cannot be opened correctly in standard spreadsheet applications like Microsoft Excel or Google Sheets. The issue occurs due to improper encoding, inconsistent delimiters, and unescaped special characters (e.g., commas, quotes, and newlines) in the exported data.
Reproduction Steps:
Call export_to_csv() with data containing special characters such as quotes, commas, or newline characters.
Attempt to open the generated file in Excel or a text editor.
Observe one or more of the following issues:
File fails to open in Excel.
Rows appear misaligned due to improper handling of delimiters.
Special characters in the data are not escaped, breaking the CSV format.
Problematic Code Example:
Issues Identified:
Improper Encoding: The default encoding may not support special characters like non-ASCII symbols, leading to corrupted files.
Delimiter Issues: Data containing commas isn’t enclosed in quotes, causing misalignment.
Lack of Escaping: Special characters (e.g., quotes and newlines) in the data are not properly escaped.
Impact:
This bug affects users exporting data with special characters or international text, resulting in unusable files or data loss.
Expected Behavior:
The text was updated successfully, but these errors were encountered: