Created
April 11, 2019 10:20
-
-
Save mde-2590/7b55d6d350191c1f2b1be6dd4e3decb4 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
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": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "--2019-04-11 10:02:57-- https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/labs/example1.txt\n", | |
| "Resolving s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)... 67.228.254.193\n", | |
| "Connecting to s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)|67.228.254.193|:443... connected.\n", | |
| "HTTP request sent, awaiting response... 200 OK\n", | |
| "Length: 45 [text/plain]\n", | |
| "Saving to: ‘/resources/data/Example1.txt’\n", | |
| "\n", | |
| "/resources/data/Exa 100%[=====================>] 45 --.-KB/s in 0s \n", | |
| "\n", | |
| "2019-04-11 10:02:57 (28.0 MB/s) - ‘/resources/data/Example1.txt’ saved [45/45]\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Download Example file\n", | |
| "\n", | |
| "!wget -O /resources/data/Example1.txt https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/labs/example1.txt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Read the Example1.txt\n", | |
| "\n", | |
| "example1 = \"/resources/data/Example1.txt\"\n", | |
| "file1 = open(example1, \"r\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'/resources/data/Example1.txt'" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Print the path of file\n", | |
| "\n", | |
| "file1.name" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'r'" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Print the mode of file, either 'r' or 'w'\n", | |
| "\n", | |
| "file1.mode" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'This is line 1 \\nThis is line 2\\nThis is line 3'" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Read the file\n", | |
| "\n", | |
| "FileContent = file1.read()\n", | |
| "FileContent\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This is line 1 \n", | |
| "This is line 2\n", | |
| "This is line 3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Print the file with '\\n' as a new line\n", | |
| "\n", | |
| "print(FileContent)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "str" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Type of file content\n", | |
| "\n", | |
| "type(FileContent)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Close file after finish\n", | |
| "\n", | |
| "file1.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This is line 1 \n", | |
| "This is line 2\n", | |
| "This is line 3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Open file using with\n", | |
| "\n", | |
| "with open(example1, \"r\") as file1:\n", | |
| " FileContent = file1.read()\n", | |
| " print(FileContent)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Verify if the file is closed\n", | |
| "\n", | |
| "file1.closed" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This is line 1 \n", | |
| "This is line 2\n", | |
| "This is line 3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# See the content of file\n", | |
| "\n", | |
| "print(FileContent)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Read first four characters\n", | |
| "\n", | |
| "with open(example1, \"r\") as file1:\n", | |
| " print(file1.read(4))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This\n", | |
| " is \n", | |
| "line 1 \n", | |
| "\n", | |
| "This is line 2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Read certain amount of characters\n", | |
| "\n", | |
| "with open(example1, \"r\") as file1:\n", | |
| " print(file1.read(4))\n", | |
| " print(file1.read(4))\n", | |
| " print(file1.read(7))\n", | |
| " print(file1.read(15))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This is line 1 \n", | |
| "\n", | |
| "This \n", | |
| "is line 2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Read certain amount of characters\n", | |
| "\n", | |
| "with open(example1, \"r\") as file1:\n", | |
| " print(file1.read(16))\n", | |
| " print(file1.read(5))\n", | |
| " print(file1.read(9))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "first line: This is line 1 \n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Read one line\n", | |
| "\n", | |
| "with open(example1, \"r\") as file1:\n", | |
| " print(\"first line: \" + file1.readline())" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Iteration 0 : This is line 1 \n", | |
| "\n", | |
| "Iteration 1 : This is line 2\n", | |
| "\n", | |
| "Iteration 2 : This is line 3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Iterate through the lines\n", | |
| "\n", | |
| "with open(example1,\"r\") as file1:\n", | |
| " i = 0;\n", | |
| " for line in file1:\n", | |
| " print(\"Iteration\", str(i), \": \", line)\n", | |
| " i = i + 1;" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Read all lines and save as a list\n", | |
| "\n", | |
| "with open(example1, \"r\") as file1:\n", | |
| " FileasList = file1.readlines()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'This is line 1 \\n'" | |
| ] | |
| }, | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Print the first line\n", | |
| "\n", | |
| "FileasList[0]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'This is line 2\\n'" | |
| ] | |
| }, | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Print the second line\n", | |
| "\n", | |
| "FileasList[1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'This is line 3'" | |
| ] | |
| }, | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Print the third line\n", | |
| "\n", | |
| "FileasList[2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "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.6.8" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment