Categories
Python Answers

How to use execfile in Python 3?

Sometimes, we want to use execfile in Python 3.

In this article, we’ll look at how to use execfile in Python 3.

How to use execfile in Python 3?

To use execfile in Python 3, we call exec.

For instance, we write

exec(open("./filename").read())

to open the ./filename file with open and read it with read.

And then we run the opened file with exec.

Conclusion

To use execfile in Python 3, we call exec.

Categories
Python Answers

How to check for NaN values with Python?

Sometimes, we want to check for NaN values with Python.

In this article, we’ll look at how to check for NaN values with Python.

How to check for NaN values with Python?

To check for NaN values with Python, we can use the math.isnan method.

For instance, we write

import math

x = float('nan')
is_nan = math.isnan(x)

to call math.isnan on x to check if x is NaN.

Since x is NaN, is_nan is True.

Conclusion

To check for NaN values with Python, we can use the math.isnan method.

Categories
Python Answers

How to strip HTML from strings in Python?

Sometimes, we want to strip HTML from strings in Python.

In this article, we’ll look at how to strip HTML from strings in Python.

How to strip HTML from strings in Python?

To strip HTML from strings in Python, we can create a subclass of the HTMLParser class.

For instance, we write

from io import StringIO
from html.parser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        super().__init__()
        self.reset()
        self.strict = False
        self.convert_charrefs= True
        self.text = StringIO()

    def handle_data(self, d):
        self.text.write(d)

    def get_data(self):
        return self.text.getvalue()

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

to create the MLStripper class.

In it, we call self.text.getvalue in the get_data method to return the HTML content without the tags.

And then in the strip_tags function, we create an MLStripper object.

We put the html in the object with feed to parse it.

And then we call s.get_data to return the parsed HTML without the tags.

Conclusion

To strip HTML from strings in Python, we can create a subclass of the HTMLParser class.

Categories
Python Answers

How to properly ignore exceptions with Python?

Sometimes, we want to properly ignore exceptions with Python.

In this article, we’ll look at how to properly ignore exceptions with Python.

How to properly ignore exceptions with Python?

To properly ignore exceptions with Python, we can use try-except.

For instance, we write

try:
    f()
except: 
    pass

to call function f in the try block.

And any exceptions raised in f will be caught in the except block.

We just add pass in the except block to make it an empty block.

Conclusion

To properly ignore exceptions with Python, we can use try-except.

Categories
Python Answers

How to add a text progress bar in terminal with block characters with Python?

Sometimes, we want to add a text progress bar in terminal with block characters with Python.

In this article, we’ll look at how to add a text progress bar in terminal with block characters with Python.

How to add a text progress bar in terminal with block characters with Python?

To add a text progress bar in terminal with block characters with Python, we can use sys.stdout.write to write block characters to the screen.

For instance, we write

import time
import sys

for i in range(100):
    time.sleep(1)
    sys.stdout.write("\r%d%%" % i)
    sys.stdout.flush()

to call sys.stdout.write to write the percentage number to the screen with "\r%d%%" % i.

And then we call flush to flush out the output.

Conclusion

To add a text progress bar in terminal with block characters with Python, we can use sys.stdout.write to write block characters to the screen.