Rajendra Dharmkar

Rajendra Dharmkar

160 Articles Published

Articles by Rajendra Dharmkar

Page 16 of 16

What are the differences between list, sequence and slice data types in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 08-May-2023 3K+ Views

We will learn about the differences between list, sequence, and slice data types in Python in this article. Lists − In Python, a list is a type of data that can hold multiple values in a single variable. You can think of it like a shopping list that contains multiple items. For example, you could have a list of numbers like [1, 2, 3, 4, 5] or a list of names like ["John", "Jane", "Bob", "Sue"]. Number_List = [1, 2, 3, 4, 5] Name_List = ["John", "Jane", "Bob", "Sue"] Sequences − In Python, a sequence is a type of data ...

Read More

How to use special characters in Python Regular Expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 13-Jun-2020 7K+ Views

From Python documentationNon-special characters match themselves. Special characters don't match themselves −\ Escape special char or start a sequence..Match any char except newline, see re.DOTALL^Match start of the string, see re.MULTILINE $ Match end of the string, see re.MULTILINE[ ]Enclose a set of matchable charsR|S Match either regex R or regex S.()Create capture group, & indicate precedenceAfter '[', enclose a set, the only special chars are −]End the set, if not the 1st char-A range, eg. a-c matches a, b or c^Negate the set only if it is the 1st char Quantifiers (append '?' for non-greedy) ...

Read More

How to get the number of capture groups in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

The following code gets the number of captured groups using Python regex in given stringExampleimport re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups())OutputThis gives the output3

Read More

How to write Python regular expression to get all the anchor tags in a webpage?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 257 Views

The following code extracts all tags in the given stringExampleimport re rex = re.compile(r'[\]') l = "this is text1 hi this is text2" print rex.findall(l)Output['', '']

Read More

How to use wildcard in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

The following code uses the Python regex .()dot character for wildcard which stands for any character other than newline.Exampleimport re rex = re.compile('th.s') l = "this, thus, just, then" print rex.findall(l)OutputThis gives the output['this', 'thus']

Read More

How to find all adverbs and their positions in a text using python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 512 Views

As per Python documentationIf one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner −>>> text = "He was carefully disguised but captured quickly by police." >>> for m in re.finditer(r"\w+ly", text): ... print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0))) 07-16: carefully 40-47: quickly

Read More

How to extract numbers from text using Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

If we want to extract all numbers/digits individually from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d', s) print resultOutput['1', '2', '3', '4', '5', '6', '7']If we want to extract groups of numbers/digits from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d+', s) print resultOutput['12345', '67']

Read More

How do I disable log messages from the Requests Python module?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 11-Dec-2019 1K+ Views

You can disable logging from the requests module using the logging module.ExampleYou can configure it to not log messages unless they are at least warnings using the following code:import logging logging.getLogger("requests").setLevel(logging.WARNING)If you want to go a level higher and only want to log messages when they are errors or critical, you can do replace logging.WARNING with logging.ERROR and logging.CRITICAL respectively.

Read More

How to use remote python modules?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 01-Oct-2019 2K+ Views

There are ways to import Python modules remotely. It is not recommended to do so though as it will slow down your app. You can use the knockout module to achieve this. To install knockout use:$ pip install knockoutNow in order to import modules remotely, you can use knockout like:>>> from knockout import urlimport >>> urlimport.register() Url importing enabled. Add urls to sys.path.A valid url looks like this: http://example.com/path/to/repository/#packagenameThis stuff is experimental, use at your own risk. Enjoy.>>> import sys >>> sys.path.insert(0, 'http://www.crummy.com/software/BeautifulSoup/download/3.x/BeautifulSoup-3.0.8/#BeautifulSoup') >>> import BeautifulSoup ... >>> BeautifulSoup If you are not able to install modules on a machine(due ...

Read More

When are python objects candidates for garbage collection?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 30-Jul-2019 315 Views

A python object or variable will be eligible for garbage collection as soon as all references to it go out of scope or are manually deleted (del x). We would have to presume there were no references to the object anywhere else for it to be garbage collected.

Read More
Showing 151–160 of 160 articles
« Prev 1 12 13 14 15 16 Next »
Advertisements