+{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"8960 Candida - Practical 3.ipynb","provenance":[],"collapsed_sections":[],"mount_file_id":"14q0TyBfW7gvJPrJBMwZow8BuKb6HaIi9","authorship_tag":"ABX9TyPYv+sl7r1FezpRlq/p6M6+"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","metadata":{"id":"g9gyUyiP5zeX"},"source":["**Name : Candida Ruth Noronha**\n","\n","**Class : SE COMPS B**\n","\n","**Roll No. : 8960**\n","\n","**Title : Python Experiment 3**"]},{"cell_type":"markdown","metadata":{"id":"oSOcPHip9-Zv"},"source":["**Aim: Write python programs to understand different file handling operations.**\n","1. Write a python program which will count the spaces, tabs, and lines in any given text file. \n","Check if the file exists. If file does not exists then handle that exception.\n","\n","**Code :**\n"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"UQpp0LuRFLZB","executionInfo":{"status":"ok","timestamp":1618214218006,"user_tz":-330,"elapsed":33552,"user":{"displayName":"8960 Candida Noronha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9oFSqr_U0fh4PF3gsP-c3esjIdm_OM-IMv4go=s64","userId":"03185904400426411648"}},"outputId":"8025189b-a9c0-466f-d0e1-cf917f8eb565"},"source":["from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Mounted at /content/drive\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"dD4qCDk65aWZ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1618218546616,"user_tz":-330,"elapsed":965,"user":{"displayName":"8960 Candida Noronha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9oFSqr_U0fh4PF3gsP-c3esjIdm_OM-IMv4go=s64","userId":"03185904400426411648"}},"outputId":"ce1600c2-daf4-477f-a383-d9168b5baf4c"},"source":["from collections import Counter\n","try:\n"," with open(\"drive/MyDrive/8960_Candida_Python/Practical 3/content.txt\") as f:\n"," text = f.read()\n"," count = Counter(text)\n","except:\n"," print(\"File not Found\")\n","else:\n"," spaces = count[\" \"]\n"," tabs = count[\"\\t\"]\n"," lines = count[\"\\n\"]\n"," print(\"The total number of spaces present in this file is \", spaces)\n"," print(\"The total number of tabs present in this file is \", tabs)\n"," print(\"The total number of lines present in this file is \", lines)\n"],"execution_count":null,"outputs":[{"output_type":"stream","text":["The total number of spaces present in this file is 114\n","The total number of tabs present in this file is 5\n","The total number of lines present in this file is 6\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"jHLXtoLOGjCK"},"source":["2. The file cities_and_times.txt contains city names and times.\n","Each line contains the name of the city, followed by the name of the day (\"Sun\") and the time in the form hh:mm. \n","Read in the file and create an alphabetically ordered list of the form\n","\n","[('Amsterdam', 'Sun', (8, 52)), ('Anchorage', 'Sat', (23, 52)), ('Ankara', 'Sun', (10, \n","52)), ('Athens', 'Sun', (9, 52)), ('Atlanta', 'Sun', (2, 52)), ('Auckland', 'Sun', (20, 52)), \n","('Barcelona', 'Sun', (8, 52)), ('Beirut', 'Sun', (9, 52)), \n","...\n","….\n","('Toronto', 'Sun', (2, 52)), ('Vancouver', 'Sun', (0, 52)), ('Vienna', 'Sun', (8, 52)), \n","('Warsaw', 'Sun', (8, 52)), ('Washington DC', 'Sun', (2, 52)), ('Winnipeg', 'Sun', (1, \n","52)), ('Zurich', 'Sun', (8, 52))]\n","Finally, the list should be dumped for later usage with the pickle module.\n","\n","**Code :**"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"add9lNLbac1n","executionInfo":{"status":"ok","timestamp":1618467222507,"user_tz":-330,"elapsed":3940,"user":{"displayName":"8960 Candida Noronha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9oFSqr_U0fh4PF3gsP-c3esjIdm_OM-IMv4go=s64","userId":"03185904400426411648"}},"outputId":"3f6cb044-f3ac-4877-ee9c-467aff538708"},"source":["import pickle\n","l=[tuple(line.strip().split(\" \")) for line in open(\"drive/MyDrive/8960_Candida_Python/Practical 3/cities_and_times.txt\")]\n","l.sort()\n","dump=open(\"drive/MyDrive/8960_Candida_Python/Practical 3/dump\",\"wb\")\n","pickle.dump(l,dump)\n","dump.close()\n","\n","load=open(\"drive/MyDrive/8960_Candida_Python/Practical 3/dump\",\"rb\")\n","print(*pickle.load(load) , sep = '\\n')"],"execution_count":1,"outputs":[{"output_type":"stream","text":["('Amsterdam', 'Sun', '08:52')\n","('Anchorage', 'Sat', '23:52')\n","('Ankara', 'Sun', '10:52')\n","('Athens', 'Sun', '09:52')\n","('Atlanta', 'Sun', '02:52')\n","('Auckland', 'Sun', '20:52')\n","('Barcelona', 'Sun', '08:52')\n","('Beirut', 'Sun', '09:52')\n","('Berlin', 'Sun', '08:52')\n","('Boston', 'Sun', '02:52')\n","('Brasilia', 'Sun', '05:52')\n","('Brussels', 'Sun', '08:52')\n","('Bucharest', 'Sun', '09:52')\n","('Budapest', 'Sun', '08:52')\n","('Cairo', 'Sun', '09:52')\n","('Calgary', 'Sun', '01:52')\n","('Cape', 'Town', 'Sun', '09:52')\n","('Casablanca', 'Sun', '07:52')\n","('Chicago', 'Sun', '01:52')\n","('Columbus', 'Sun', '02:52')\n","('Copenhagen', 'Sun', '08:52')\n","('Dallas', 'Sun', '01:52')\n","('Denver', 'Sun', '01:52')\n","('Detroit', 'Sun', '02:52')\n","('Dubai', 'Sun', '11:52')\n","('Dublin', 'Sun', '07:52')\n","('Edmonton', 'Sun', '01:52')\n","('Frankfurt', 'Sun', '08:52')\n","('Halifax', 'Sun', '03:52')\n","('Helsinki', 'Sun', '09:52')\n","('Houston', 'Sun', '01:52')\n","('Indianapolis', 'Sun', '02:52')\n","('Istanbul', 'Sun', '10:52')\n","('Jerusalem', 'Sun', '09:52')\n","('Johannesburg', 'Sun', '09:52')\n","('Kathmandu', 'Sun', '13:37')\n","('Kuwait', 'City', 'Sun', '10:52')\n","('Las', 'Vegas', 'Sun', '00:52')\n","('Lisbon', 'Sun', '07:52')\n","('London', 'Sun', '07:52')\n","('Los', 'Angeles', 'Sun', '00:52')\n","('Madrid', 'Sun', '08:52')\n","('Melbourne', 'Sun', '18:52')\n","('Miami', 'Sun', '02:52')\n","('Minneapolis', 'Sun', '01:52')\n","('Montreal', 'Sun', '02:52')\n","('Moscow', 'Sun', '10:52')\n","('New', 'Orleans', 'Sun', '01:52')\n","('New', 'York', 'Sun', '02:52')\n","('Oslo', 'Sun', '08:52')\n","('Ottawa', 'Sun', '02:52')\n","('Paris', 'Sun', '08:52')\n","('Philadelphia', 'Sun', '02:52')\n","('Phoenix', 'Sun', '00:52')\n","('Prague', 'Sun', '08:52')\n","('Reykjavik', 'Sun', '07:52')\n","('Riyadh', 'Sun', '10:52')\n","('Rome', 'Sun', '08:52')\n","('Salt', 'Lake', 'City', 'Sun', '01:52')\n","('San', 'Francisco', 'Sun', '00:52')\n","('San', 'Salvador', 'Sun', '01:52')\n","('Santiago', 'Sun', '04:52')\n","('Seattle', 'Sun', '00:52')\n","('Shanghai', 'Sun', '15:52')\n","('Singapore', 'Sun', '15:52')\n","('Stockholm', 'Sun', '08:52')\n","('Sydney', 'Sun', '18:52')\n","('São', 'Paulo', 'Sun', '05:52')\n","('Tokyo', 'Sun', '16:52')\n","('Toronto', 'Sun', '02:52')\n","('Vancouver', 'Sun', '00:52')\n","('Vienna', 'Sun', '08:52')\n","('Warsaw', 'Sun', '08:52')\n","('Washington', 'DC', 'Sun', '02:52')\n","('Winnipeg', 'Sun', '01:52')\n","('Zurich', 'Sun', '08:52')\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"FsFyUoxch2Uu"},"source":["**Post Labs**"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"HdxhXzglh4so","executionInfo":{"status":"ok","timestamp":1618222160746,"user_tz":-330,"elapsed":1343,"user":{"displayName":"8960 Candida Noronha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9oFSqr_U0fh4PF3gsP-c3esjIdm_OM-IMv4go=s64","userId":"03185904400426411648"}},"outputId":"90601531-a4be-41e1-c972-1095878370ad"},"source":["def longestword(filename):\n"," with open(\"drive/MyDrive/8960_Candida_Python/Practical 3/computer.txt\") as f:\n"," words = f.read().split()\n"," max_len_word = max(words,key=len)\n"," max_len = len(max(words,key=len))\n"," print('The word in the file with maximum length is :',max_len_word)\n"," print('And its length is : ',max_len)\n","longestword(\"text.txt\")"],"execution_count":null,"outputs":[{"output_type":"stream","text":["The word in the file with maximum length is : automatically.\n","And its length is : 14\n"],"name":"stdout"}]}]}
0 commit comments