Categories
Python Answers

How to set sys.stdout encoding in Python 3?

Sometimes, we want to set sys.stdout encoding in Python.

In this article, we’ll look at how to set sys.stdout encoding in Python.

How to set sys.stdout encoding in Python 3?

To set sys.stdout encoding in Python, we can call the sys.stdout.reconfigure method.

For instance, we write

sys.stdout.reconfigure(encoding='utf-8')

to call sys.stdout.reconfigure with the encoding argument set to 'utf-8'.

This will change the stdoud encoding to utf-8.

Conclusion

To set sys.stdout encoding in Python, we can call the sys.stdout.reconfigure method.

Categories
Python Answers

How to change list order from os.listdir() with Python?

Sometimes, we want to change list order from os.listdir() with Python.

In this article, we’ll look at how to change list order from os.listdir() with Python.

How to change list order from os.listdir() with Python?

To change list order from os.listdir() with Python, we can call sorted on the iterator returned by listdir.

For instance, we write

l = sorted(os.listdir(whatever_directory))

to call sorted on the iterator that’s returned by os.listdir.

We call os.listdir with the root directory path to get the items inside it.

Conclusion

To change list order from os.listdir() with Python, we can call sorted on the iterator returned by listdir.

Categories
Python Answers

How to check if a float value is a whole number with Python?

Sometimes, we want to check if a float value is a whole number with Python.

In this article, we’ll look at how to check if a float value is a whole number with Python.

How to check if a float value is a whole number with Python?

To check if a float value is a whole number with Python, we can use the is_integer method.

For instance, we write

is_integer = (1.0).is_integer()

to check if 1.0 is an integer by calling is_integer on it.

It’ll return True if it is and False otherwise.

Conclusion

To check if a float value is a whole number with Python, we can use the is_integer method.

Categories
Python Answers

How to create column output in Python?

Sometimes, we want to create column output in Python.

In this article, we’ll look at how to create column output in Python.

How to create column output in Python?

To create column output in Python, we can call format on a format string.

For instance, we write

table_data = [
    ['a', 'b', 'c'],
    ['aaaaaaaaaa', 'b', 'c'], 
    ['a', 'bbbbbbbbbb', 'c']
]

for row in table_data:
    print("{: >20} {: >20} {: >20}".format(*row))

to loop through table_data and call print in the loop with the format string to format the items in row into neatly formatted columns.

We use {: >20} to add placeholders for a column column space.

Conclusion

To create column output in Python, we can call format on a format string.

Categories
Python Answers

How to use XPath with BeautifulSoup and Python?

To use XPath with BeautifulSoup and Python, we can replace BeautifulSoup with lxml.

For instance, we write

from lxml import html
import requests

page = requests.get('http://example.com')
tree = html.fromstring(page.content)
names = tree.xpath('//div[@title="name"]/text()')
prices = tree.xpath('//span[@class="price"]/text()')

print('Names: ', names)
print('Prices: ', prices)

to call html.fromstring to parse the HTML string into an object.

Then we call xpath with the XPath to get the items by XPath.