Created
September 5, 2016 19:11
-
-
Save wesm/dcc68d323c76a13c0f4a3bd5bfc8b36b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import csv\n", | |
"import pandas as pd\n", | |
"\n", | |
"df = pd.DataFrame({\"text\": [\"\"\"Hello! Please \"help\" me. I cannot quote a csv.\\\\\"\"\"], \"zoo\": [\"1\"]})\n", | |
"df.to_csv(\"out.csv\", index=False, quoting=csv.QUOTE_NONNUMERIC, encoding=\"utf-8\", escapechar='\\\\', doublequote=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Hello! Please \"help\" me. I cannot quote a csv.\\\\'" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"badval = df.text[0]\n", | |
"badval" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\"text\",\"zoo\"\r\n", | |
"\"Hello! Please \\\"help\\\" me. I cannot quote a csv.\\\",\"1\"\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!cat out.csv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Hello! Please \"help\" me. I cannot quote a csv.\",1\"'" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df2 = pd.read_csv(\"out.csv\", quoting=csv.QUOTE_NONNUMERIC, encoding=\"utf-8\", escapechar='\\\\', doublequote=False)\n", | |
"df2.text[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Hello! Please \"help\" me. I cannot quote a csv.\\\\'" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"badval" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"with open('out2.csv', 'w', encoding='utf-8') as f:\n", | |
" writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC, escapechar=\"\\\\\", doublequote=False)\n", | |
" writer.writerow((badval,))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\"Hello! Please \\\"help\\\" me. I cannot quote a csv.\\\"\r", | |
"\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!cat out2.csv" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment