Last active
September 5, 2016 19:14
-
-
Save wesm/7763d396ae25c9fd5b27588da27015e4 to your computer and use it in GitHub Desktop.
This file contains 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": 30, | |
"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, \"1\"))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\"Hello! Please \\\"help\\\" me. I cannot quote a csv.\\\",\"1\"\r", | |
"\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!cat out2.csv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['Hello! Please \"help\" me. I cannot quote a csv.\",1\"']\n" | |
] | |
} | |
], | |
"source": [ | |
"with open('out2.csv', 'r', encoding='utf-8') as f:\n", | |
" reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC, escapechar=\"\\\\\", doublequote=False)\n", | |
" print(next(reader))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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