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.

Categories
Python Answers

How to suppress scientific notation when printing float values with Python?

Sometimes, we want to suppress scientific notation when printing float values with Python.

In this article, we’ll look at how to suppress scientific notation when printing float values with Python.

How to suppress scientific notation when printing float values with Python?

To suppress scientific notation when printing float values with Python, we can use the string’s format method with the {:f} format code.

For instance, we write:

a = -7.1855143557448603e-17
s = '{:f}'.format(a)
print(s)

We call format with a as the argument.

{:f} will format the number so that all the digits are in the string.

Therefore, s is -0.000000.

Conclusion

To suppress scientific notation when printing float values with Python, we can use the string’s format method with the {:f} format code.

Categories
Python Answers

How to convert [key1,val1,key2,val2] to a dict with Python?

Sometimes, we want to convert [key1,val1,key2,val2] to a dict with Python.

In this article, we’ll look at how to convert [key1,val1,key2,val2] to a dict with Python.

How to convert [key1,val1,key2,val2] to a dict with Python?

To convert [key1,val1,key2,val2] to a dict with Python, we can use the zip and dict functions.

For instance, we write:

a = ['foo', 1, 'bar', 2]
b = dict(zip(a[::2], a[1::2]))

print(b)

We call zip with the slices a that returns the keys and values.

We get all the keys with a[::2].

And we get all the values with a[1::2].

Then we call zip to combine them into a list of key-value tuples.

And then we call dict to convert the list of tuples into a dictionary.

Therefore, b is {'foo': 1, 'bar': 2}.

Conclusion

To convert [key1,val1,key2,val2] to a dict with Python, we can use the zip and dict functions.