Categories
Python Answers

How to convert binary to ASCII and vice versa with Python?

Sometimes, we want to convert binary to ASCII and vice versa with Python.

In this article, we’ll look at how to convert binary to ASCII and vice versa with Python.

How to convert binary to ASCII and vice versa with Python?

To convert binary to ASCII and vice versa with Python, we can use the binascii module.

To convert ASCII to binary, we write:

import binascii

b = bin(int(binascii.hexlify(b'hello'), 16))
print(b)

We call binascii.hexlify with b'hello' to convert the binary string to hex.

Then we call int to convert the hex to a decimal integer.

And then we call bin to convert the int to a binary string.

Therefore, b is 0b110100001100101011011000110110001101111.

To convert binary to ASCII, we call binascii.unhexlify.

For instance, we write:

import binascii

n = int('0b110100001100101011011000110110001101111', 2)
s = binascii.unhexlify('%x' % n)
print(s)

We call int with a binary string and 2 as the base.

Then we call binascii.unhexlify with '%x' % n to convert the int to a binary string.

Therefore s is b'hello'.

Conclusion

To convert binary to ASCII and vice versa with Python, we can use the binascii module.

Categories
Python Answers

How to make asynchronous requests with Python requests module?

Sometimes, we want to make asynchronous requests with Python.

In this article, we’ll look at how to make asynchronous requests with Python.

How to make asynchronous requests with Python?

To make asynchronous requests with Python, we can use the grequests module.

For instance, we write:

import grequests

urls = [
    'http://www.heroku.com',
    'http://tablib.org',
    'http://httpbin.org',
    'http://python-requests.org',
    'http://kennethreitz.com'
]

rs = (grequests.get(u) for u in urls)
r = grequests.map(rs)
print(r)

We call grequests.get with the entries in the urls list.

And then we assign the responses to rs.

Then we call grequests.map with rs to make all the requests at once and assign the list of responses to r.

Therefore, r is [<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>].

Conclusion

To make asynchronous requests with Python, we can use the grequests module.

Categories
Python Answers

How to get the filename without the extension from a path in Python?

Sometimes, we want to get the filename without the extension from a path in Python.

In this article, we’ll look at how to get the filename without the extension from a path in Python.

How to get the filename without the extension from a path in Python?

To get the filename without the extension from a path in Python, we can use the os.path.splittext method.

For instance, we write:

import os

print(os.path.splitext("/path/to/some/file.txt")[0])

We call os.path.splittext with the "/path/to/some/file.txt" path string.

Then we get the path to the file without the extension with index 0 from the returned list.

Therefore, print should print '/path/to/some/file'.

Conclusion

To get the filename without the extension from a path in Python, we can use the os.path.splittext method.

Categories
Python Answers

Is there a zip-like function that pads to longest length in Python?

Sometimes, we want to use a zip-like function that pads to longest length in Python to zip 2 lists.

In this article, we’ll look at how to use a zip-like function that pads to longest length in Python to zip 2 lists.

Is there a zip-like function that pads to longest length in Python?

To use a zip-like function that pads to longest length in Python to zip 2 lists, we can use the itertools.zip_longest method.

For instance, we write:

import itertools

a = ['a1']
b = ['b1', 'b2', 'b3']
c = ['c1', 'c2']
zipped = list(itertools.zip_longest(a, b, c))
print(zipped)

We have 3 lists a, b, and c.

Then we call itertools.zip_longest with the 3 lists.

And then we convert the iterator back to a list with list and assign the list to zipped.

Therefore, zipped is:

[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]

Conclusion

To use a zip-like function that pads to longest length in Python to zip 2 lists, we can use the itertools.zip_longest method.

Categories
Python Answers

How to convert timestamps with offset to datetime obj using Python’s strptime?

Sometimes, we want to convert timestamps with offset to datetime obj using Python’s strptime method.

In this article, we’ll look at how to convert timestamps with offset to datetime obj using Python’s strptime method.

How to convert timestamps with offset to datetime obj using Python’s strptime?

To convert timestamps with offset to datetime obj using Python’s strptime method, we can call isoformat after parsing the datetime string.

For instance, we write:

from datetime import datetime

time_str = "2021-07-24T23:14:29-07:00"
dt_aware = datetime.fromisoformat(time_str)
print(dt_aware.isoformat('T'))

We call datetime.fromisoformat method with the time_str datetime string.

Then we call isoformat with T to put the 'T‘ between the date and the time of the returned datetime string.

Therefore, print will print '2021-07-24T23:14:29-07:00'.

Conclusion

To convert timestamps with offset to datetime obj using Python’s strptime method, we can call isoformat after parsing the datetime string.