Python – Spelling Check ”; Previous Next Checking of spelling is a basic requirement in any text processing or analysis. The python package pyspellchecker provides us this feature to find the words that may have been mis-spelled and also suggest the possible corrections. First, we need to install the required package using the following command in our python environment. pip install pyspellchecker Now we see below how the package is used to point out the wrongly spelled words as well as make some suggestions about possible correct words. from spellchecker import SpellChecker spell = SpellChecker() # find those words that may be misspelled misspelled = spell.unknown([”let”, ”us”, ”wlak”,”on”,”the”,”groun”]) for word in misspelled: # Get the one `most likely` answer print(spell.correction(word)) # Get a list of `likely` options print(spell.candidates(word)) When we run the above program we get the following output − group {”group”, ”ground”, ”groan”, ”grout”, ”grown”, ”groin”} walk {”flak”, ”weak”, ”walk”} Case Sensitive If we use Let in place of let then this becomes a case sensitive comparison of the word with the closest matched words in dictionary and the result looks different now. from spellchecker import SpellChecker spell = SpellChecker() # find those words that may be misspelled misspelled = spell.unknown([”Let”, ”us”, ”wlak”,”on”,”the”,”groun”]) for word in misspelled: # Get the one `most likely` answer print(spell.correction(word)) # Get a list of `likely` options print(spell.candidates(word)) When we run the above program we get the following output − group {”groin”, ”ground”, ”groan”, ”group”, ”grown”, ”grout”} walk {”walk”, ”flak”, ”weak”} get {”aet”, ”ret”, ”get”, ”cet”, ”bet”, ”vet”, ”pet”, ”wet”, ”let”, ”yet”, ”det”, ”het”, ”set”, ”et”, ”jet”, ”tet”, ”met”, ”fet”, ”net”} Print Page Previous Next Advertisements ”;
Category: python Text Processing
Python – String Immutability
Python – String Immutability ”; Previous Next In python, the string data types are immutable. Which means a string value cannot be updated. We can verify this by trying to update a part of the string which will led us to an error. # Can not reassign t= “Tutorialspoint” print type(t) t[0] = “M” When we run the above program, we get the following output − t[0] = “M” TypeError: ”str” object does not support item assignment We can further verify this by checking the memory location address of the position of the letters of the string. . x = ”banana” for idx in range (0,5): print x[idx], “=”, id(x[idx]) When we run the above program we get the following output. As you can see above a and a point to same location. Also N and N also point to the same location. b = 91909376 a = 91836864 n = 91259888 a = 91836864 n = 91259888 Print Page Previous Next Advertisements ”;
Python – Text Processing Introduction ”; Previous Next Text processing has a direct application to Natural Language Processing, also known as NLP. NLP is aimed at processing the languages spoken or written by humans when they communicate with one another. This is different from the communication between a computer and a human where the communication is wither a computer program written by human or some gesture by human like clicking the mouse at some position. NLP tries to understand the natural language spoken by humans and classify it, analyses it as well if required respond to it. Python has a rich set of libraries which cater to the needs of NLP. The Natural Language Tool Kit (NLTK) is a suite of such libraries which provides the functionalities required for NLP. Below are some applications which use NLP and indirectly python”s NLTK. Summarization Many times, we need to get the summary of a news article, a movie plot or a big story. They are all written in human language and without NLP we have to rely on another human”s interpretation and presentation of such summary to us. But with help of NLP we can write programs to use NLTK and summarize the long text with various parameters, like what is the percentage of text we want in the final output, choosing the positive and negative words for summarization etc. The online news feeds rely on such summarization techniques to present news insights. Voice Based Tools The voice-based tools like apples Siri or Amazon Alexa rely on NLP to understand the interaction mad with humans. They have a large training data set of words, sentences and grammar to interpret the question or command coming from a human and process it. Though it is about voice, indirectly it also gets translated to text and the resulting text form the voice is taken through the NLP system to produce result. Information Extraction Web scrapping is a common example of extracting data form the web pages using python code. Here it may not be strictly NLP based but it does involve text processing. For example, if we need to extract only the headers present in a html page, then we look for the h1 tag int he page structure and find a way to extract the text between only those tags. This need text processing program from python. Spam Filtering The spam in emails can be identified and eliminated by analysing the text in the subject line as well as in the content of the message. As the spam emails are usually sent in bulk to many recipients, even if their subjects and contents have little variation, that can be matched and tagged to mark them as spam Again it needs the use of the NLTK libraries. Language Translation Computerized language translation relies heavily on NLP. As more and more languages are used in the online platform, it becomes a necessity to automate the translation from one human language to another. This will involve programming to handle the vocabulary, grammar and context tagging of the languages involved in translation. Again, NLTK is used to handle such requirements. Sentiment Analysis To find out the overall reaction to the performance of a movie, we may have to read thousands of feedback posts from the audience. But that too can be automated by using the classification of positive an negative feedback through words and sentence analysis. And then measuring the frequency of positive and negative reviews to find the overall sentiment of the audience. This obviously needs the analysis of the human language written by the audience and NLTK is used heavily here for processing the text. Print Page Previous Next Advertisements ”;
Python – Text wrapping
Python – Text wrapping ”; Previous Next Text wrapping is required when the text grabbed from some source is not properly formatted to be displayed within the available screen width. This is achieved by using the below package which can be installed in our environment with below command. pip install parawrap The below paragraph has a single string of text which is continuous. on applying the wrap function we can see how the text is separated into multiple lines separated with commas. import parawrap text = “In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleone”s daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughter”s wedding day. One of the men who asks the Don for a favor is Amerigo Bonasera, a successful mortician and acquaintance of the Don, whose daughter was brutally beaten by two young men because she refused their advances; the men received minimal punishment from the presiding judge. The Don is disappointed in Bonasera, who”d avoided most contact with the Don due to Corleone”s nefarious business dealings. The Don”s wife is godmother to Bonasera”s shamed daughter, a relationship the Don uses to extract new loyalty from the undertaker. The Don agrees to have his men punish the young men responsible (in a non-lethal manner) in return for future service if necessary.” print parawrap.wrap(text) When we run the above program we get the following output − [”In late summer 1945, guests are gathered for the wedding reception of”, “Don Vito Corleone”s daughter Connie (Talia Shire) and Carlo Rizzi”, ”(Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia”, ”family, is known to friends and associates as Godfather. He and Tom”, ”Hagen (Robert Duvall), the Corleone family lawyer, are hearing”, ”requests for favors because, according to Italian tradition, no”, “Sicilian can refuse a request on his daughter”s wedding day. One of”, ”the men who asks the Don for a favor is Amerigo Bonasera, a successful”, ”mortician and acquaintance of the Don, whose daughter was brutally”, ”beaten by two young men because she refused their advances; the men”, ”received minimal punishment from the presiding judge. The Don is”, “disappointed in Bonasera, who”d avoided most contact with the Don due”, “to Corleone”s nefarious business dealings. The Don”s wife is godmother”, “to Bonasera”s shamed daughter, a relationship the Don uses to extract”, ”new loyalty from the undertaker. The Don agrees to have his men punish”, ”the young men responsible (in a non-lethal manner) in return for”, ”future service if necessary.”] We can also apply the wrap function with specific width as input parameter which will cut the words if required to maintain the required width of the wrap function. import parawrap text = “In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleone”s daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughter”s wedding day. One of the men who asks the Don for a favor is Amerigo Bonasera, a successful mortician and acquaintance of the Don, whose daughter was brutally beaten by two young men because she refused their advances; the men received minimal punishment from the presiding judge. The Don is disappointed in Bonasera, who”d avoided most contact with the Don due to Corleone”s nefarious business dealings. The Don”s wife is godmother to Bonasera”s shamed daughter, a relationship the Don uses to extract new loyalty from the undertaker. The Don agrees to have his men punish the young men responsible (in a non-lethal manner) in return for future service if necessary.” print parawrap.wrap(text,5) When we run the above program we get the following output − [”In”, ”late ”, ”summe”, ”r”, ”1945,”, ”guest”, ”s are”, ”gathe”, ”red”, ”for”, ”the w”, ”eddin”, ”g rec”, ”eptio”, ”n of”, ”Don”, ”Vito ”, ”Corle”, “one”s”, ”daugh”, ”ter C”, ”onnie”, ”(Tali”, ”a Shi”, ”re)”, ”and”, ”Carlo”, ”Rizzi”, ”(Gian”, ”ni Ru”, ”sso).”, ”Vito ”, ”(Marl”, ”on Br”, ”ando)”, ”, the”, ”head”, ”of”, ”the C”, ”orleo”, ”ne”, ”Mafia”, ”famil”, ”y, is”, ”known”, ”to fr”, ”iends”, ”and a”, ”ssoci”, ”ates”, ”as Go”, ”dfath”, ”er.”, ”He”, ”and”, ”Tom”, ”Hagen”, ”(Robe”, ”rt Du”, ”vall)”, ”, the”, ”Corle”, ”one f”, ”amily”, ”lawye”, ”r,”, ”are h”, ”earin”, ”g req”, ”uests”, ”for f”, ”avors”, ”becau”, ”se, a”, ”ccord”, ”ing”, ”to It”, ”alian”, ”tradi”, ”tion,”, ”no Si”, ”cilia”, ”n can”, ”refus”, ”e a r”, ”eques”, ”t on”, ”his d”, ”aught”, “er”s “, ”weddi”, ”ng”, ”day.”, ”One”, ”of”, ”the”, ”men”, ”who”, ”asks”, ”the”, ”Don”, ”for a”, ”favor”, ”is Am”, ”erigo”, ”Bonas”, ”era,”, ”a suc”, ”cessf”, ”ul mo”, ”rtici”, ”an”, ”and a”, ”cquai”, ”ntanc”, ”e of”, ”the”, ”Don,”, ”whose”, ”daugh”, ”ter”, ”was b”, ”rutal”, ”ly be”, ”aten”, ”by”, ”two”, ”young”, ”men b”, ”ecaus”, ”e she”, ”refus”, ”ed”, ”their”, ”advan”, ”ces;”, ”the”, ”men r”, ”eceiv”, ”ed mi”, ”nimal”, ”punis”, ”hment”, ”from”, ”the p”, ”resid”, ”ing j”, ”udge.”, ”The”, ”Don”, ”is di”, ”sappo”, ”inted”, ”in Bo”, ”naser”, ”a,”, “who”d”, ”avoid”, ”ed”, ”most ”, ”conta”, ”ct”, ”with”, ”the”, ”Don”, ”due”, ”to Co”, ”rleon”, “e”s n”, ”efari”, ”ous b”, ”usine”, ”ss de”, ”aling”, ”s.”, ”The”, “Don”s”, ”wife”, ”is go”, ”dmoth”, ”er to”, ”Bonas”, “era”s”, ”shame”, ”d dau”, ”ghter”, ”, a r”, ”elati”, ”onshi”, ”p the”, ”Don”, ”uses”, ”to ex”, ”tract”, ”new l”, ”oyalt”, ”y”, ”from”, ”the u”, ”ndert”, ”aker.”, ”The”, ”Don a”, ”grees”, ”to”, ”have”, ”his”, ”men p”, ”unish”, ”the”, ”young”, ”men r”, ”espon”, ”sible”, ”(in a”, ”non-l”, ”ethal”, ”manne”, ”r) in”, ”retur”, ”n for”, ”futur”, ”e ser”, ”vice”, ”if ne”, ”cessa”, ”ry.”] Print Page
Python – Text Processing
Python – Text Processing Python Programming can be used to process text data for the requirements in various textual data analysis. A very important area of application of such text processing ability of python is for NLP (Natural Language Processing). NLP is used in search engines, newspaper feed analysis and more recently for voice -based applications like Siri and Alexa. Python”s Natural Language Toolkit (NLTK) is a group of libraries that can be used for creating such Text Processing systems. Audience This tutorial is designed for Computer Science graduates as well as Software Professionals who are willing to learn Text Processing in simple and easy steps using Python as a programming language. Prerequisites Before proceeding with this tutorial, you should have a basic knowledge of writing code in Python programming language, using any python IDE and execution of Python programs. If you are completely new to python then please refer our Python tutorial to get a sound understanding of the language. Print Page Previous Next Advertisements ”;
Python – Text Processing Environment ”; Previous Next To successfully create and run the example code in this tutorial we will need an environment set up which will have both general-purpose python as well as the special packages required for Data science. We will first look as installing the general-purpose python which can be python 2 or python 3. But we will prefer python 2 for this tutorial mainly because of its maturity and wider support of external packages. Getting Python The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official website of Python https://www.python.org/ You can download Python documentation from https://www.python.org/doc/. The documentation is available in HTML, PDF, and PostScript formats. Installing Python Python distribution is available for a wide variety of platforms. You need to download only the binary code applicable for your platform and install Python. If the binary code for your platform is not available, you need a C compiler to compile the source code manually. Compiling the source code offers more flexibility in terms of choice of features that you require in your installation. Here is a quick overview of installing Python on various platforms − Unix and Linux Installation Here are the simple steps to install Python on Unix/Linux machine. Open a Web browser and go to https://www.python.org/downloads/. Follow the link to download zipped source code available for Unix/Linux. Download and extract files. Editing the Modules/Setup file if you want to customize some options. run ./configure script make make install This installs Python at standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXX where XX is the version of Python. Windows Installation Here are the steps to install Python on Windows machine. Open a Web browser and go to https://www.python.org/downloads/. Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to install. To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI. Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished, and you are done. Macintosh Installation Recent Macs come with Python installed, but it may be several years out of date. See http://www.python.org/download/mac/ for instructions on getting the current version along with extra tools to support development on the Mac. For older Mac OS”s before Mac OS X 10.3 (released in 2003), MacPython is available. Jack Jansen maintains it and you can have full access to the entire documentation at his website − http://www.cwi.nl/~jack/macpython.html. You can find complete installation details for Mac OS installation. Setting up PATH Programs and other executable files can be in many directories, so operating systems provide a search path that lists the directories that the OS searches for executables. The path is stored in an environment variable, which is a named string maintained by the operating system. This variable contains information available to the command shell and other programs. The path variable is named as PATH in Unix or Path in Windows (Unix is case sensitive; Windows is not). In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory, you must add the Python directory to your path. Setting path at Unix/Linux To add the Python directory to the path for a particular session in Unix − In the csh shell − type setenv PATH “$PATH:/usr/local/bin/python” and press Enter. In the bash shell (Linux) − type export ATH=”$PATH:/usr/local/bin/python” and press Enter. In the sh or ksh shell − type PATH=”$PATH:/usr/local/bin/python” and press Enter. Note − /usr/local/bin/python is the path of the Python directory Setting path at Windows To add the Python directory to the path for a particular session in Windows − At the command prompt − type path %path%;C:Python and press Enter. Note − C:Python is the path of the Python directory Python Environment Variables Here are important environment variables, which can be recognized by Python − Sr.No. Variable & Description 1 PYTHONPATH It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer. 2 PYTHONSTARTUP It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH. 3 PYTHONCASEOK It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it. 4 PYTHONHOME It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy. Running Python There are three different ways to start Python − Interactive Interpreter You can start Python from Unix, DOS, or any other system that provides you a command-line interpreter or shell window. Enter python the command line. Start coding right away in the interactive interpreter. $python # Unix/Linux or python% # Unix/Linux or C:> python # Windows/DOS Here is the list of all the available command line options − Sr.No. Option & Description 1 -d It provides debug output. 2 -O It generates optimized bytecode (resulting in .pyo files). 3 -S Do not run import site to look for Python paths on startup. 4 -v verbose output (detailed trace on import statements). 5 -X disable class-based built-in exceptions (just use strings); obsolete starting with version 1.6. 6 -c cmd run Python script sent in as cmd string 7 file run Python script from given file Script from the Command-line A Python script can be executed at command line by invoking the interpreter on your application, as in the
Python – Counting Token in Paragraphs ”; Previous Next While reading the text from a source, sometimes we also need to find out some statistics about the type of words used. That makes it necessary to count the number of words as well as lines with a specific type of words in a given text. In the below example we show programs to count the words in a paragraph using two different approaches. We consider a text file for this purpose which contains the summary of a Hollywood movie. Reading the File FileName = (“PathGodFather.txt”) with open(FileName, ”r”) as file: lines_in_file = file.read() print lines_in_file When we run the above program we get the following output − Vito Corleone is the aging don (head) of the Corleone Mafia Family. His youngest son Michael has returned from WWII just in time to see the wedding of Connie Corleone (Michael”s sister) to Carlo Rizzi. All of Michael”s family is involved with the Mafia, but Michael just wants to live a normal life. Drug dealer Virgil Sollozzo is looking for Mafia families to offer him protection in exchange for a profit of the drug money. He approaches Don Corleone about it, but, much against the advice of the Don”s lawyer Tom Hagen, the Don is morally against the use of drugs, and turns down the offer. This does not please Sollozzo, who has the Don shot down by some of his hit men. The Don barely survives, which leads his son Michael to begin a violent mob war against Sollozzo and tears the Corleone family apart. Counting Words Using nltk Next we use the nltk module to count the words in the text. Please note the word ”(head)” is counted as 3 words and not one. import nltk FileName = (“PathGodFather.txt”) with open(FileName, ”r”) as file: lines_in_file = file.read() nltk_tokens = nltk.word_tokenize(lines_in_file) print nltk_tokens print “n” print “Number of Words: ” , len(nltk_tokens) When we run the above program we get the following output − [”Vito”, ”Corleone”, ”is”, ”the”, ”aging”, ”don”, ”(”, ”head”, ”)”, ”of”, ”the”, ”Corleone”, ”Mafia”, ”Family”, ”.”, ”His”, ”youngest”, ”son”, ”Michael”, ”has”, ”returned”, ”from”, ”WWII”, ”just”, ”in”, ”time”, ”to”, ”see”, ”the”, ”wedding”, ”of”, ”Connie”, ”Corleone”, ”(”, ”Michael”, “”s”, ”sister”, ”)”, ”to”, ”Carlo”, ”Rizzi”, ”.”, ”All”, ”of”, ”Michael”, “”s”, ”family”, ”is”, ”involved”, ”with”, ”the”, ”Mafia”, ”,”, ”but”, ”Michael”, ”just”, ”wants”, ”to”, ”live”, ”a”, ”normal”, ”life”, ”.”, ”Drug”, ”dealer”, ”Virgil”, ”Sollozzo”, ”is”, ”looking”, ”for”, ”Mafia”, ”families”, ”to”, ”offer”, ”him”, ”protection”, ”in”, ”exchange”, ”for”, ”a”, ”profit”, ”of”, ”the”, ”drug”, ”money”, ”.”, ”He”, ”approaches”, ”Don”, ”Corleone”, ”about”, ”it”, ”,”, ”but”, ”,”, ”much”, ”against”, ”the”, ”advice”, ”of”, ”the”, ”Don”, “”s”, ”lawyer”, ”Tom”, ”Hagen”, ”,”, ”the”, ”Don”, ”is”, ”morally”, ”against”, ”the”, ”use”, ”of”, ”drugs”, ”,”, ”and”, ”turns”, ”down”, ”the”, ”offer”, ”.”, ”This”, ”does”, ”not”, ”please”, ”Sollozzo”, ”,”, ”who”, ”has”, ”the”, ”Don”, ”shot”, ”down”, ”by”, ”some”, ”of”, ”his”, ”hit”, ”men”, ”.”, ”The”, ”Don”, ”barely”, ”survives”, ”,”, ”which”, ”leads”, ”his”, ”son”, ”Michael”, ”to”, ”begin”, ”a”, ”violent”, ”mob”, ”war”, ”against”, ”Sollozzo”, ”and”, ”tears”, ”the”, ”Corleone”, ”family”, ”apart”, ”.”] Number of Words: 167 Counting Words Using Split Next we count the words using Split function and here the word ”(head)” is counted as a single word and not 3 words as in case of using nltk. FileName = (“PathGodFather.txt”) with open(FileName, ”r”) as file: lines_in_file = file.read() print lines_in_file.split() print “n” print “Number of Words: “, len(lines_in_file.split()) When we run the above program we get the following output − [”Vito”, ”Corleone”, ”is”, ”the”, ”aging”, ”don”, ”(head)”, ”of”, ”the”, ”Corleone”, ”Mafia”, ”Family.”, ”His”, ”youngest”, ”son”, ”Michael”, ”has”, ”returned”, ”from”, ”WWII”, ”just”, ”in”, ”time”, ”to”, ”see”, ”the”, ”wedding”, ”of”, ”Connie”, ”Corleone”, “(Michael”s”, ”sister)”, ”to”, ”Carlo”, ”Rizzi.”, ”All”, ”of”, “Michael”s”, ”family”, ”is”, ”involved”, ”with”, ”the”, ”Mafia,”, ”but”, ”Michael”, ”just”, ”wants”, ”to”, ”live”, ”a”, ”normal”, ”life.”, ”Drug”, ”dealer”, ”Virgil”, ”Sollozzo”, ”is”, ”looking”, ”for”, ”Mafia”, ”families”, ”to”, ”offer”, ”him”, ”protection”, ”in”, ”exchange”, ”for”, ”a”, ”profit”, ”of”, ”the”, ”drug”, ”money.”, ”He”, ”approaches”, ”Don”, ”Corleone”, ”about”, ”it,”, ”but,”, ”much”, ”against”, ”the”, ”advice”, ”of”, ”the”, “Don”s”, ”lawyer”, ”Tom”, ”Hagen,”, ”the”, ”Don”, ”is”, ”morally”, ”against”, ”the”, ”use”, ”of”, ”drugs,”, ”and”, ”turns”, ”down”, ”the”, ”offer.”, ”This”, ”does”, ”not”, ”please”, ”Sollozzo,”, ”who”, ”has”, ”the”, ”Don”, ”shot”, ”down”, ”by”, ”some”, ”of”, ”his”, ”hit”, ”men.”, ”The”, ”Don”, ”barely”, ”survives,”, ”which”, ”leads”, ”his”, ”son”, ”Michael”, ”to”, ”begin”, ”a”, ”violent”, ”mob”, ”war”, ”against”, ”Sollozzo”, ”and”, ”tears”, ”the”, ”Corleone”, ”family”, ”apart.”] Number of Words: 146 Print Page Previous Next Advertisements ”;
Python – Reformatting Paragraphs ”; Previous Next Formatting of paragraphs is needed when we deal with large amount of text and bring it to a presentable format. We may just want to print each line with specific width or try to increase the indentation for each next line when printing a poem. In this chapter we use a module named as textwrap3 to format the paragraphs as needed. First we need to install the required package as follows pip install textwrap3 Wrapping to a Fixed Width In this example we specify a width of 30 characters in each line for a paragraph. Use the wrap function by specifying a value for the width parameter. from textwrap3 import wrap text = ”In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughters wedding day.” x = wrap(text, 30) for i in range(len(x)): print(x[i]) When we run the above program we get the following output − In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughters wedding day. Variable Indention In this example we increase the indent for each line of a poem to be printed. import textwrap3 FileName = (“pathpoem.txt”) print(“**Before Formatting**”) print(” “) data=file(FileName).readlines() for i in range(len(data)): print data[i] print(” “) print(“**After Formatting**”) print(” “) data=file(FileName).readlines() for i in range(len(data)): dedented_text = textwrap3.dedent(data[i]).strip() print dedented_text When we run the above program we get the following output − **Before Formatting** Summer is here. Sky is bright. Birds are gone. Nests are empty. Where is Rain? **After Formatting** Summer is here. Sky is bright. Birds are gone. Nests are empty. Where is Rain? Print Page Previous Next Advertisements ”;
Python – Sorting Lines
Python – Sorting Lines ”; Previous Next Many times, we need to sort the content of a file for analysis. For example, we want to get the sentences written by different students to get arranged in the alphabetical order of their names. That will involve sorting just not by the first character of the line but also all the characters starting from the left. In the below program we first read the lines from a file then print them using the sort function which is part of the standard python library. Printing the File FileName = (“pathpoem.txt”) data=file(FileName).readlines() for i in range(len(data)): print data[i] When we run the above program, we get the following output − Summer is here. Sky is bright. Birds are gone. Nests are empty. Where is Rain? Sorting Lines in the File Now we apply the sort function before printing the content of the file. the lines get sorted as per the first alphabet form the left. FileName = (“pathpoem.txt”) data=file(FileName).readlines() data.sort() for i in range(len(data)): print data[i] When we run the above program, we get the following output − Birds are gone. Nests are empty. Sky is bright. Summer is here. Where is Rain? Print Page Previous Next Advertisements ”;
Python – Extract Emails from Text ”; Previous Next To extract emails form text, we can take of regular expression. In the below example we take help of the regular expression package to define the pattern of an email ID and then use the findall() function to retrieve those text which match this pattern. import re text = “Please contact us at [email protected] for further information.”+ ” You can also give feedbacl at [email protected]” emails = re.findall(r”[a-z0-9.-+_]+@[a-z0-9.-+_]+.[a-z]+”, text) print emails When we run the above program, we get the following output − [”[email protected]”, ”[email protected]”] Print Page Previous Next Advertisements ”;