Categories
Python Answers

How to read streaming input from Python subprocess.communicate()?

Sometimes, we want to read streaming input from Python subprocess.communicate() method.

In this article, we’ll look at how to read streaming input from Python subprocess.communicate() method.

How to read streaming input from Python subprocess.communicate()?

To read streaming input from Python subprocess.communicate() method, we can use the subprocess.Popen method.

For instance, we write:

from subprocess import Popen, PIPE

with Popen(["ls", "-l"], stdout=PIPE, bufsize=1,
           universal_newlines=True) as p:
    for line in p.stdout:
        print(line, end='')

We call Popen with the command and argument in the list.

And we set stdout to PIPE to pipe the output to p.stdout.

Then we loop through each line in p.stdout and print them with print.

Conclusion

To read streaming input from Python subprocess.communicate() method, we can use the subprocess.Popen method.

Categories
Python Answers

How to remove accents (normalize) in a Python unicode string?

Sometimes, we want to remove accents (normalize) in a Python unicode string.

In this article, we’ll look at how to remove accents (normalize) in a Python unicode string.

How to remove accents (normalize) in a Python unicode string?

To remove accents (normalize) in a Python unicode string, we can use the unicodedata.normalize method.

For instance, we write:

import unicodedata


def strip_accents(s):
    return ''.join(c for c in unicodedata.normalize('NFD', s)
                   if unicodedata.category(c) != 'Mn')
no_accent = strip_accents(u"A \u00c0 \u0394 \u038E")      
print(no_accent)             

We call unicodedata.normalize on the s string and then join all the returned letters in the list with join.

We filter out all the non-spacing characters in s with if unicodedata.category(c) != 'Mn'

Therefore, no_accent is 'A A Δ Υ'.

Conclusion

To remove accents (normalize) in a Python unicode string, we can use the unicodedata.normalize method.

Categories
Python Answers

How to do a case-insensitive string comparison with Python?

Sometimes, we want to do a case-insensitive string comparison with Python.

In this article, we’ll look at how to do a case-insensitive string comparison with Python.

How to do a case-insensitive string comparison with Python?

To do a case-insensitive string comparison with Python, we can convert both strings we want to compare to lower case.

For instance, we write:

string1 = 'Hello'
string2 = 'hello'

if string1.casefold() == string2.casefold():
    print("The strings are the same (case insensitive)")
else:
    print("The strings are NOT the same (case insensitive)")

We call the casefold method on both strings and compare them with ==.

Therefore, we should see 'The strings are the same (case insensitive)' printed since both strings are the same ignoring the case.

Conclusion

To do a case-insensitive string comparison with Python, we can convert both strings we want to compare to lower case.

Categories
Python Answers

How to decode HTML entities in a Python string?

Sometimes, we to decode HTML entities in a Python string.

In this article, we’ll look at how to decode HTML entities in a Python string.

How to decode HTML entities in a Python string?

To decode HTML entities in a Python string, we can use the Beautiful Soup library.

To install it, we run:

pip install bs4

Then we write:

from bs4 import BeautifulSoup

html = BeautifulSoup("<p>&pound;682m</p>")
print(html)

We instantiate the BeautifulSoup class with a string with some HTML entities in it.

Then we assign the returned object to html.

Therefore, html is '<p>£682m</p>'.

Conclusion

To decode HTML entities in a Python string, we can use the Beautiful Soup library.

Categories
Python Answers

How to print lists as tabular data with Python?

Sometimes, we want to print lists as tabular data with Python.

In this article, we’ll look at how to print lists as tabular data with Python.

How to print lists as tabular data with Python?

To print lists as tabular data with Python, we can use the tabulate library.

We install it by running:

pip install tabulate

For instance, we write:

from tabulate import tabulate
print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))

We import the tabulate function from the tabulate module.

Then we call tabulate with a nested list and set the headers parameter to a list of headers for each row.

Therefore, we get:

Name      Age
------  -----
Alice      24
Bob        19

printed as a result.

Conclusion

To print lists as tabular data with Python, we can use the tabulate library.