2015-01-28 02:45:34 +08:00
{
2015-05-14 18:55:23 +08:00
" cells " : [
2015-06-19 09:07:36 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-11-01 19:44:00 +08:00
" This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/data-science-ipython-notebooks). "
2015-06-19 09:07:36 +08:00
]
} ,
2015-05-14 18:55:23 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Files \n " ,
" \n " ,
" * Read a File \n " ,
" * Write a File \n " ,
" * Read and Write UTF-8 "
]
} ,
2015-01-28 02:45:34 +08:00
{
2015-05-14 18:55:23 +08:00
" 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 " : [
2015-01-28 02:45:34 +08:00
{
2015-05-14 18:55:23 +08:00
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" class TypeUtil: \n " ,
2015-01-28 02:45:34 +08:00
" \n " ,
2015-05-14 18:55:23 +08:00
" @classmethod \n " ,
" def is_iterable(cls, obj): \n " ,
" \" \" \" Determines if obj is iterable. \n " ,
2015-01-28 02:45:34 +08:00
" \n " ,
2015-05-14 18:55:23 +08:00
" 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 " ,
2015-01-28 02:45:34 +08:00
" \n " ,
2015-05-14 18:55:23 +08:00
" @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 "
2015-02-17 06:01:10 +08:00
]
2015-01-28 02:45:34 +08:00
}
] ,
2015-05-14 18:55:23 +08:00
" 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 ' ) "
]
2015-01-28 02:45:34 +08:00
}
2015-05-14 18:55:23 +08:00
] ,
" 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 " ,
2015-06-19 09:07:36 +08:00
" version " : " 2.7.10 "
2015-05-14 18:55:23 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}