From 4f3ab9431f522c29ed2726483c0ed6921439efa4 Mon Sep 17 00:00:00 2001 From: bharatr95 <109859835+bharatr95@users.noreply.github.com> Date: Fri, 29 Jul 2022 18:36:44 +0530 Subject: [PATCH] Add files via upload --- ...andling, Exception Handling-Solution.ipynb | 1445 +++++++++++++++++ 1 file changed, 1445 insertions(+) create mode 100644 Assignment on Modules, File Handling, Exception Handling-Solution.ipynb diff --git a/Assignment on Modules, File Handling, Exception Handling-Solution.ipynb b/Assignment on Modules, File Handling, Exception Handling-Solution.ipynb new file mode 100644 index 0000000..57af445 --- /dev/null +++ b/Assignment on Modules, File Handling, Exception Handling-Solution.ipynb @@ -0,0 +1,1445 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "7dcd5b62", + "metadata": {}, + "source": [ + "## Modules - Included questions from different internal/external libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdee71d3", + "metadata": {}, + "outputs": [], + "source": [ + "# Data is availble on cloud.\n", + "# apI call --> JSON --> python dictionary\n", + "\n", + "# Flask ???" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad0ceae7", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "86d9827c", + "metadata": {}, + "outputs": [], + "source": [ + "#Mo1 Hint: use json library/module\n", + "Write a Python program to convert data in a given json file to the following format.\n", + "(Find json file attached)\n", + "Input: Given JSON File (https://drive.google.com/file/d/15zl_FaXdatnlLlciBSrYKdiyaTvEATbO/view)\n", + "Output:\n", + " header.title = \"This is main\"\n", + " header.subtitle = \"This is subtext\"\n", + " footer.chapter = \"Chapter Title\"\n", + " footer.page = \"1\"\n", + " footer.revision = \"First\"\n", + " body.content = \"This is a paragraph\"" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "2c867617", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{ \n", + " \"header\": {\n", + " \"title\": \"This is main\",\n", + " \"subtitle\": \"This is subtext\"\n", + " },\n", + " \"footer\": {\n", + " \"chapter\": \"Chapter title\",\n", + " \"page\": \"1\",\n", + " \"revision\": \"First\"\n", + " },\n", + " \"body\": {\n", + " \"chapter\": \"This is a paragraph\"\n", + " \n", + " }\n", + "}\n", + "\n" + ] + } + ], + "source": [ + "import json\n", + "\n", + "f = open(\"check.json\", \"r\")\n", + "print(f.read())\n", + "f.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "455242a6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'header': {'title': 'This is main', 'subtitle': 'This is subtext'}, 'footer': {'chapter': 'Chapter title', 'page': '1', 'revision': 'First'}, 'body': {'chapter': 'This is a paragraph'}}\n" + ] + } + ], + "source": [ + "import json\n", + "\n", + "f = open(\"check.json\", \"r\")\n", + "data_ = json.load(f) # USE This a lot to show the JSON content\n", + "print(data_)\n", + "f.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "id": "87142971", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is main\n", + "This is subtext\n", + "Chapter title\n", + "1\n", + "First\n", + "This is a paragraph\n" + ] + } + ], + "source": [ + "import json\n", + "\n", + "f = open(\"check.json\", \"r\")\n", + "data_ = json.load(f) # USE This a lot to show the JSON content\n", + "# print(data_)\n", + "f.close()\n", + "\n", + "\n", + "for i in data_ : \n", + " for j in data_[i] : \n", + " print(data_[i][j])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3a9a40e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "78c55f65", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting pytube\n", + " Downloading pytube-12.1.0-py3-none-any.whl (56 kB)\n", + "Installing collected packages: pytube\n", + "Successfully installed pytube-12.1.0\n" + ] + } + ], + "source": [ + "# !pip install pytube" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "c0df8223", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\Nitish\\\\Desktop\\\\PP\\\\Batch, 29.05.22, Weekend\\\\Assignments\\\\JSON Tutorial in Python.mp4'" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Mo2\n", + "# Write a program to download specific video from YouTube and save it inside computer.\n", + "# Take video link as input from the user.\n", + "\n", + "# Next Step : Take search name from the user and show relevant youtube video names rto the user and download that video.\n", + "\n", + "from pytube import YouTube\n", + "url_ = \"https://www.youtube.com/watch?v=jABj-SEhtBc\"\n", + "\n", + "yt = YouTube(url_)\n", + "\n", + "ys = yt.streams.get_highest_resolution()\n", + "ys.download()\n", + "\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "30910586", + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install playsound\n", + "\n", + "\n", + "import playsound" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "id": "101783e4", + "metadata": {}, + "outputs": [], + "source": [ + "#Mo3\n", + "# Play the Music.\n", + "\n", + "# import playsound\n", + "\n", + "# if int(input(\"Mention Number\")) % 5 == 0 : \n", + "# print(\"divisible by 5\")\n", + " \n", + "# playsound.playsound(\"Audio1.mp3\")\n", + " \n", + "# else : \n", + "# print(\"Not multiple of 5\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "id": "f6f31594", + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install pexpect" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "156825a3", + "metadata": {}, + "outputs": [], + "source": [ + "# import pexpect\n", + "# import time\n", + "# i=1\n", + "# while i<=10:\n", + "# num=int(input(\"Enter 10 numbers\"))\n", + "# if num%5==0:\n", + "# child=pexpect.spawn(\"vlc '10 second intro music-jRWR0Ob6mLI.mp4'\")\n", + "# time.sleep(2)\n", + "# child.sendcontrol('c')\n", + "# i+=1\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4fa1c43e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 125, + "id": "f7ce97fb", + "metadata": {}, + "outputs": [], + "source": [ + "# pyttsx3\n", + "# speech_recognition\n", + "\n", + "# For Speech Projects...." + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "id": "01823212", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pytz in c:\\users\\nitish\\anaconda3\\lib\\site-packages (2020.1)\n" + ] + } + ], + "source": [ + "!pip install pytz" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "e9d8cebf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter timezone: Asia/Kolkata\n", + "Timezone is Asia/Kolkata\n", + "Asia/Kolkata in default format: 2022-07-24 11:20:08.227457+05:30\n", + "Date & Time in Asia/Kolkata 2022:07:24 11:20:08 IST +0530\n" + ] + } + ], + "source": [ + "#Mo4\n", + "# Write a program to print the current time and date in a timezone which is taken as input from the user.\n", + "\n", + "from datetime import datetime\n", + "import pytz\n", + "\n", + "\n", + "# pradeep**\n", + "tzi = input(\"Enter timezone: \")\n", + "tz = pytz.timezone(tzi)\n", + "\n", + "print(\"Timezone is\", tz)\n", + "print(tz, \"in default format: \", datetime.now(tz))\n", + "print(\"Date & Time in\", tz, datetime.now(tz).strftime('%Y:%m:%d %H:%M:%S %Z %z'))\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "99930b1d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Timezones\n", + "Timezone naive: 2022-07-24 11:19:22.669477\n", + "Timezone Aware: 2022-07-24 05:49:22.669477+00:00\n", + "Please enter the timezone you need to check: Asia/Kolkata\n", + "Time zone 2022-07-24 11:19:29.603953+05:30\n" + ] + } + ], + "source": [ + "# sandeep **\n", + "from datetime import datetime\n", + "import pytz\n", + "\n", + "print('Timezones')\n", + "# for timeZone in pytz.all_timezones:\n", + "# print(timeZone)\n", + "\n", + " \n", + "# current Datetime\n", + "unaware = datetime.now()\n", + "print('Timezone naive:', unaware)\n", + "\n", + "# Standard UTC timezone aware Datetime\n", + "aware = datetime.now(pytz.utc)\n", + "print('Timezone Aware:', aware)\n", + "\n", + "timez=input(\"Please enter the timezone you need to check: \") \n", + " \n", + "# timezone datetime\n", + "Newone = datetime.now(pytz.timezone(timez))\n", + "print('Time zone', Newone)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2e0af6dc", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 137, + "id": "4311b06c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "#Mo5\n", + "# Write a program to tell the week number of a date taken as input from the user.\n", + "# For e.g.: \n", + "# Input: '13/02/2022'\n", + "# Output: 6\n", + "\n", + "\n", + "inputDate='13/02/2022'\n", + "from datetime import datetime\n", + "dt=datetime.strptime(inputDate,'%d/%m/%Y')\n", + "print(dt.weekday())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 143, + "id": "b0f215d0", + "metadata": {}, + "outputs": [], + "source": [ + "# dt.strftime(\"%b\")" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "id": "ef6f63c4", + "metadata": {}, + "outputs": [], + "source": [ + "# datetime.now().strftime(\"%d\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d0306e71", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8547eead", + "metadata": {}, + "outputs": [], + "source": [ + "#Mo6\n", + "# Write a program to send a specific mail to person using gmail.\n", + "# Take mail content from a txt file stored in a computer\n", + "# Take receiver email address as input from the user.\n", + "# Send the email from a set gmail account. (Use dummy account here if needed.)\n", + "\n", + "\n", + "# * gmail has increased the security" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d0fb520", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12baf048", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9c5352a0", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8c21100", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "bcbcadd8", + "metadata": {}, + "source": [ + "## File Handling" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "d8ed575f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 20\n" + ] + } + ], + "source": [ + "a, b = [10, 20]\n", + "print(a, b)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "f17ef2a4", + "metadata": {}, + "outputs": [], + "source": [ + "#F1\n", + "# WAP to read content from one file and create two separate files from the given file content. \n", + "# The given file has two paragraphs separated by '-----------'. \n", + "# Save these two paragraphs in two separate files.\n", + "\n", + "ifd=open('myFile.txt','r')\n", + "inputStr=ifd.read()\n", + "outputfile1Str , outputfile2Str = inputStr.split('-----------')\n", + "ifd.close()\n", + "\n", + "of1d=open('outputfile1.txt','w')\n", + "of1d.write(outputfile1Str)\n", + "of1d.close()\n", + "\n", + "of2d=open('outputfile2.txt','w')\n", + "of2d.write(outputfile2Str)\n", + "of2d.close()\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "34c52f62", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "efmhewgiewufhguerbhijgfbi ref 2156r2 3r26 t2t32 ewrT2e\n", + " 2ew\n", + " ew3 ew3f\n", + " ew3f \n", + " 43f\n", + " er3f\n", + " 3gerf3erf2e3rf2er*frf2refre\n", + " e\n", + " \n", + " \n", + "\n", + " \n", + " wefgdfqwftdqbhj r 2r15 62wf3 f2\\ \n", + " f3 \n", + " fe3f \n", + " e3f \n", + " 3f\n", + " ffewb vlfm nkhfghwfe\n", + " 25ewf ewe fef \n", + " eefe\n", + " w f62511-6213-65\n" + ] + } + ], + "source": [ + "of1d=open('outputfile1.txt','r')\n", + "print(of1d.read())\n", + "of1d.close()\n", + "\n", + "of2d=open('outputfile2.txt','r')\n", + "print(of2d.read())\n", + "of2d.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ed2eb1e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5bde643d", + "metadata": {}, + "outputs": [], + "source": [ + "#F2\n", + "# Write a program to save the dictionary inside a file in json style.\n", + "# The dictionary is containing multiple details about a book such as author, date, pages, mrp, etc...\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "c8b550e9", + "metadata": {}, + "outputs": [], + "source": [ + "# #F3\n", + "# Write a program to read specific lines from a given txt file. Take line number as input from the user." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "2027d93b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the line number to read8\n", + " e\n", + "\n" + ] + } + ], + "source": [ + "# pradeep *\n", + "f=open('myFile.txt','r')\n", + "linenum = int(input(\"Enter the line number to read\") )\n", + "\n", + "lines=f.readlines() # list of all lines in the file\n", + "\n", + "print(lines[linenum-1])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "4ef0997b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "******************************\n", + "Enter the line# to read: 14\n", + " fe3f \n", + "\n" + ] + } + ], + "source": [ + "# bala **\n", + "f = open(\"myFile.txt\",\"r\")\n", + "data_ = f.readlines()\n", + "print(\"*\" * 30)\n", + "f.close()\n", + "\n", + "\n", + "num = int(input(\"Enter the line# to read: \")) - 1\n", + "print( data_[num] if num >0 and num < len(data_) else \"Enter the correct line#\" ) \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "543e2728", + "metadata": {}, + "outputs": [], + "source": [ + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "5ac87eb5", + "metadata": {}, + "outputs": [], + "source": [ + "#F4\n", + "# Write a Python program that takes a text file as input and returns the number of words present in the file." + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "0715d5a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "33\n" + ] + } + ], + "source": [ + "# pradeep\n", + "f=open('myFile.txt','r')\n", + "content=f.read()\n", + "wordlist=content.split()\n", + "print(len(wordlist))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "2a0d8077", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "33" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len( open('myFile.txt','r').read().split() )" + ] + }, + { + "cell_type": "markdown", + "id": "92fea34f", + "metadata": {}, + "source": [ + "#### With will open the file and once the block have been executed, it close down the file\n", + "\n", + "- r+ : opens the file in reading and writing mode\n", + "- w+ : opens the file in writing and reading mode\n", + "- a+ : opens the file in appending and reading mode\n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "084ac393", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HiEarth\n" + ] + } + ], + "source": [ + "with open(\"s1.txt\", \"w+\") as f : \n", + " f.write(\"Hi\")\n", + " f.write(\"Earth\")\n", + " \n", + " f.seek(0)\n", + " print(f.read())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "4bf21c71", + "metadata": {}, + "outputs": [], + "source": [ + "# f.write(\"sjhcsbcuk\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1043434f", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "24aeb995", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['efmhewgiewufhguerbhijgfbi', 'ref', '2156r2', '3r26', 't2t32', 'ewrT2e']\n", + "['2ew']\n", + "['ew3', 'ew3f']\n", + "['ew3f']\n", + "['43f']\n", + "['er3f']\n", + "['3gerf3erf2e3rf2er*frf2refre']\n", + "['e']\n", + "[]\n", + "['-----------']\n", + "[]\n", + "['wefgdfqwftdqbhj', 'r', '2r15', '62wf3', 'f2\\\\']\n", + "['f3']\n", + "['fe3f']\n", + "['e3f']\n", + "['3f']\n", + "['ffewb', 'vlfm', 'nkhfghwfe']\n", + "['25ewf', 'ewe', 'fef']\n", + "['eefe']\n", + "['w', 'f62511-6213-65']\n", + "33\n" + ] + } + ], + "source": [ + "with open('myFile.txt','r+') as f:\n", + " numw=0\n", + " for line in f:\n", + " words= line.split()\n", + " print(words)\n", + " numw += len(words)\n", + " print(numw)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64ae7447", + "metadata": {}, + "outputs": [], + "source": [ + "open( , \"r\")\n", + "\n", + "close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f135f73", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3adea0ed", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "75dd6980", + "metadata": {}, + "outputs": [], + "source": [ + "#F5\n", + "# Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to Z.txt.\n", + "# Save the multiplication table of the corresponding number in the corresponding text file.\n", + "# For e.g.: Table of 1 in A.txt, Table of 2 in B.txt and so on...\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "f9fc7b5c", + "metadata": {}, + "outputs": [], + "source": [ + "# n = 5\n", + "# for i in range(1, 11) : \n", + "# print(f\"{n} * {i} = {n * i}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "588fade9", + "metadata": {}, + "outputs": [], + "source": [ + "def table(n) : \n", + " s = \"\"\n", + " for i in range(1, 11) : \n", + " s = s + f\"{n} * {i} = {n * i}\" + \"\\n\"\n", + " \n", + " return s" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "ba0acfea", + "metadata": {}, + "outputs": [], + "source": [ + "# print(table(5))" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "116d43a1", + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(1, 27) : \n", + "# print(chr(i))\n", + " \n", + " fName = f\"{chr(64 + i)}.txt\"\n", + " f = open(fName, 'w')\n", + " \n", + " s = table(i)\n", + " f.write(f\"Multiplication Table of {i}\")\n", + " f.write(s)\n", + " \n", + " f.close()\n", + " \n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "d39b2b6f", + "metadata": {}, + "outputs": [], + "source": [ + "# Bala**\n", + "\n", + "import string\n", + "\n", + "for i,j in enumerate(string.ascii_uppercase):\n", + " filename = j + '.txt'\n", + " f = open(filename,\"a\")\n", + " \n", + " num = i+1\n", + " #WTITE THE FILE\n", + " for i in range (1,11):\n", + " data_ = str(num) + \" x \" + str(i) + \" = \" + str(num*i)\n", + " f.write(data_ + \"\\n\")\n", + " f.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "597da6c6", + "metadata": {}, + "outputs": [], + "source": [ + "fName = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "1f220a76", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Z'" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chr(90)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cb38ca10", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "b4fdc68a", + "metadata": {}, + "source": [ + "## Regular Expression" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3685cc23", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "--------------------------------------------------\n", + "False\n" + ] + } + ], + "source": [ + "#Re1\n", + "# Write a Python program where a string will start with a specific number.\n", + "\n", + "# For e.g.: Check for string starting with 9\n", + "# Input: 91\n", + "# Output: True\n", + " \n", + "# Input: 83\n", + "# Output: False\n", + "\n", + "# pradeep**\n", + "import re\n", + "str1=\"91 hello python\"\n", + "str2=\"83 hi python\"\n", + "res=re.match(r'9\\w+',str1)\n", + "if res:\n", + " print(\"True\")\n", + "else:\n", + " print(\"False\")\n", + "print(\"-\"*50)\n", + "res=re.match(r'9\\w+',str2)\n", + "if res:\n", + " print(\"True\")\n", + "else:\n", + " print(\"False\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3d0ab2a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter your Number : 45\n", + "Yes it is a number : 45\n" + ] + } + ], + "source": [ + "# ajit**\n", + "\n", + "string =input(\"Enter your Number : \")\n", + "\n", + "res = re.match(\"\\d+\",string)\n", + "res\n", + "if res:\n", + " print(\"Yes it is a number : \", res.group())\n", + "else:\n", + " print(\"No Match\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ec47fd30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# bala **\n", + "import re\n", + "inp = \"91aa\"\n", + "print(\"True\") if re.match(r\"9\\w+\",inp) != None else print(\"False\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "16046514", + "metadata": {}, + "outputs": [], + "source": [ + "#Re2\n", + "# Write a Python program to search some substrings in a string.\n", + "\n", + "# For e.g.: \"Python is a great language and easy for beginners\"\n", + "# Input: \"Python\", \"easy\", \"best\"\n", + "# Output: \"Matched\", \"Matched\", \"Not Matched\"\n", + " \n", + "\n", + "stringName = \"Python is a great language and easy for beginners\" \n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1e1ef63b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Matched\n", + "Matched\n", + "Not Matched\n" + ] + } + ], + "source": [ + "# pradeep**\n", + "def findSubStr(substr,mainstr):\n", + " if re.search(substr,str1):\n", + " print(\"Matched\")\n", + " else:\n", + " print(\"Not Matched\")\n", + " \n", + "str1=\"Python is a great language and easy for beginners\"\n", + "findSubStr(\"Python\",str1)\n", + "findSubStr(\"easy\",str1)\n", + "findSubStr(\"best\",str1)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "7a974524", + "metadata": {}, + "outputs": [], + "source": [ + "# bala**\n", + "mystring = \"Python is a great language and easy for beginners\" \n", + "\n", + "while True: \n", + " if inp.lower() == \"exit\":\n", + " break\n", + " inp = input(\"Enter the search string: \")\n", + " print(\"Matched\") if re.search(rf\"{inp}\",mystring) != None else print(\"Not Matched\")\n", + " print(\"*\"*30)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "dd65789f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Matched\n" + ] + } + ], + "source": [ + "# ajit*\n", + "\n", + "string = 'Python is a great language and easy for beginners'\n", + "substring = 'great'\n", + "if re.findall(substring, string):\n", + " print(\"Matched\")\n", + "else:\n", + " print(\"Not Matched\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "23be3507", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not Matched\n", + "Completed\n" + ] + } + ], + "source": [ + "mystr=\"Python is a great language and easy for beginners\"\n", + "try:\n", + " for each in mystr.split():\n", + " res=re.search(r\"pppp\",mystr)\n", + " res.group()\n", + "\n", + " print(\"Matched\")\n", + "except Exception as e:\n", + " print('Not Matched')\n", + " \n", + "finally:\n", + " print(\"Completed\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c1094235", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "59d77cb7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Python_is_a_great_language_and_easy_for_beginners'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Re3\n", + "# Write a Python program to replace whitespaces with an underscore.\n", + "\n", + "# For e.g.: \n", + "# Input: \"Python is a great language and easy for beginners\"\n", + "# Output: \"Python_is_a_great_language_and_easy_for_beginners\"\n", + " \n", + "Input = \"Python is a great language and easy for beginners\"\n", + "\n", + "import re\n", + "\n", + "re.sub( \"\\s\", \"_\", Input)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "e42b8e2a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'216.8.94.196'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Re4\n", + "# Write a Python program to remove leading zeros from an IP address.\n", + "\n", + "# For e.g.:\n", + "# Input: \"216.008.094.196\"\n", + "# Output: \"216.8.94.196\"\n", + " \n", + "Input = \"216.008.094.196\"\n", + "\n", + "# re.sub(r'\\.[0]*', '.', Input)\n", + "\n", + "# re.sub(\"[.][0]+\",\".\",Input)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22f965fd", + "metadata": {}, + "outputs": [], + "source": [ + "#Re4\n", + "# Write a Python program to find the type of IP Address\n", + "\n", + "# For e.g.:\n", + "# Input: 192.0.2.126\n", + "# Output: IPv4\n", + "\n", + "# Input: 3001:0da8:75a3:0000:0000:8a2e:0370:7334\n", + "# Output: IPv6\n", + "\n", + "# Input: 36.12.08.20.52\n", + "# Output: Neither\n", + " \n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef784368", + "metadata": {}, + "outputs": [], + "source": [ + "#Re5 Hint: Use external library OS\n", + "# Write a Python program to find files having a particular extension\n", + "\n", + "# For e.g.:\n", + "# Input: Different files present in a particular folder. Files are of different types\n", + "# Output: Make a list of all the files having .txt extension\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "249cf937", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['HW. Learn 3rd.txt', 'Python practice links.txt']\n" + ] + } + ], + "source": [ + "textfiles=[]\n", + "for file in os.listdir():\n", + " txtregex=re.compile(r'[^/]+\\.txt') #Since unix allowing any special chars also in filename except /\n", + " res=txtregex.match(file)\n", + " if res:\n", + " textfiles.append(file)\n", + "print(textfiles)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "63354dbe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + ".done\n", + ".ipynb_checkpoints\n", + ".jovianrc\n", + "Additional Python Assignment.ipynb\n", + "Additional Python Solutions.ipynb\n", + "Assignment on Modules, File Handling, Exception Handling-Solution.ipynb\n", + "Assignment on Modules, File Handling, Exception Handling.ipynb\n", + "Conditional_and_loop_assignment Solution.ipynb\n", + "find pos of 3rd repition element.py\n", + "HW. Learn 3rd.txt\n", + "List_Assignment.ipynb\n", + "Python practice links.txt\n", + "Regular Expression-Assignment.ipynb\n", + "Regular Expression-Solution.ipynb\n" + ] + } + ], + "source": [ + "import os\n", + "import re\n", + "\n", + "data_ = os.listdir()\n", + "# print(data_)\n", + "for files in data_:\n", + " if re.match(\"\\w+.txt$\",files) != None:\n", + " print(files)\n", + " pass\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ee186a34", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}