Python Articles

Page 821 of 855

What is the correct way to define class variables in Python?

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

Class variables are variables that are declared outside the__init__method. These are static elements, meaning, they belong to the class rather than to the class instances. These class variables are shared by all instances of that class. Example code for class variables Exampleclass MyClass:   __item1 = 123   __item2 = "abc"   def __init__(self):     #pass or something elseYou'll understand more clearly with more code −class MyClass:     stat_elem = 456     def __init__(self):         self.object_elem = 789 c1 = MyClass() c2 = MyClass() # Initial values of both elements >>> print c1.stat_elem, c1.object_elem 456 ...

Read More

How to read and write unicode (UTF-8) files in Python?

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

The io module is now recommended and is compatible with Python 3's open syntax: The following code is used to read and write to unicode(UTF-8) files in PythonExampleimport io with io.open(filename,'r',encoding='utf8') as f: text = f.read() # process Unicode text with io.open(filename,'w',encoding='utf8') as f: f.write(text)

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 246 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 489 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 to write a Python regular expression to match multiple words anywhere?

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

The following code using Python regex matches the given multiple words in the given stringExampleimport re s = "These are roses and lilies and orchids, but not marigolds or .." r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X) print r.findall(s)OutputThis gives the output['roses', 'orchids', 'marigolds']

Read More

How to find and replace within a text file using Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 19-Feb-2020 482 Views

The following code does the replacement in the given text file. After the replacement, the text is written to a new text file 'bar.txt'Examplef1 = open('foo.txt', 'r') f2 = open('bar.txt', 'w') for line in f1:     print line     f2.write(line.replace('Poetry', 'Prose')) f2 = open('bar.txt', 'r') for line in f2:    print line, f1.close() f2.close()OutputThis gives the outputPoetry is often considered the oldest form of literature. Poetry today is usually  written down, but is still sometimes performed. Prose is often considered the oldest form of literature. Prose today is usually written down, but is still sometimes performed.

Read More

What are Python modules for date manipulation?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 19-Feb-2020 310 Views

There are many modules available in both the standard library and the PiPy repository for date manipulation. The most popular among these libraries are the following(in no particular order) −datetime (Standard library) − The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.time(Standard library) − This module provides various time-related functions. Although this module is always available, not all functions are available on all platforms. Most of the functions defined in this ...

Read More
Showing 8201–8210 of 8,547 articles
« Prev 1 819 820 821 822 823 855 Next »
Advertisements