mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
136 lines
3.2 KiB
Plaintext
136 lines
3.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Files\n",
|
|
"\n",
|
|
"* Read a File\n",
|
|
"* Write a File\n",
|
|
"* Read and Write UTF-8"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Read a File\n",
|
|
"\n",
|
|
"Open a file in read-only mode.<br\\>\n",
|
|
"Iterate over the file lines. rstrip removes the EOL markers.<br\\>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"class TypeUtil:\n",
|
|
"\n",
|
|
" @classmethod\n",
|
|
" def is_iterable(cls, obj):\n",
|
|
" \"\"\"Determines if obj is iterable.\n",
|
|
"\n",
|
|
" Useful when writing functions that can accept multiple types of\n",
|
|
" input (list, tuple, ndarray, iterator). Pairs well with\n",
|
|
" convert_to_list.\n",
|
|
" \"\"\"\n",
|
|
" try:\n",
|
|
" iter(obj)\n",
|
|
" return True\n",
|
|
" except TypeError:\n",
|
|
" return False\n",
|
|
"\n",
|
|
" @classmethod\n",
|
|
" def convert_to_list(cls, obj):\n",
|
|
" \"\"\"Converts obj to a list if it is not a list and it is iterable,\n",
|
|
" else returns the original obj.\n",
|
|
" \"\"\"\n",
|
|
" if not isinstance(obj, list) and cls.is_iterable(obj):\n",
|
|
" obj = list(obj)\n",
|
|
" return obj\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"old_file_path = 'type_util.py'\n",
|
|
"with open(old_file_path, 'r') as old_file:\n",
|
|
" for line in old_file:\n",
|
|
" print(line.rstrip())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Write to a file\n",
|
|
"\n",
|
|
"Create a new file overwriting any previous file with the same name, write text, then close the file:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"new_file_path = 'hello_world.txt'\n",
|
|
"with open(new_file_path, 'w') as new_file:\n",
|
|
" new_file.write('hello world!')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Read and Write UTF-8"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import codecs\n",
|
|
"with codecs.open(\"hello_world_new.txt\", \"a\", \"utf-8\") as new_file:\n",
|
|
" with codecs.open(\"hello_world.txt\", \"r\", \"utf-8\") as old_file: \n",
|
|
" for line in old_file:\n",
|
|
" new_file.write(line + '\\n')"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 2",
|
|
"language": "python",
|
|
"name": "python2"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|