Categories
Python Answers

How to format floats without trailing zeros with Python?

Sometimes, we want to format floats without trailing zeros with Python.

In this article, we’ll look at how to format floats without trailing zeros with Python.

How to format floats without trailing zeros with Python?

To format floats without trailing zeros with Python, we can use the rstrip method.

For instance, we write:

x = 3.14000
n = ('%f' % x).rstrip('0').rstrip('.')
print(n)

We interpolate x into a string and then call rstrip with 0 and '.' to remove trailing zeroes from the number strings.

Therefore, n is 3.14.

Conclusion

To format floats without trailing zeros with Python, we can use the rstrip method.

Categories
Python Answers

How to tail a log file in Python?

Sometimes, we want to tail a log file in Python.

In this article, we’ll look at how to tail a log file in Python.

How to tail a log file in Python?

To tail a log file in Python, we can run tail with the sh module.

For instance, we write:

from sh import tail

for line in tail("-f", "foo.txt", _iter=True):
    print(line)

We call tail with the file name and _iter set to True to let us run an infinite loop and print out line which has the output.

Conclusion

To tail a log file in Python, we can run tail with the sh module.

Categories
Python Answers

How to find the average of a list with Python?

Sometimes, we want to find the average of a list with Python.

In this article, we’ll look at how to find the average of a list with Python.

How to find the average of a list with Python?

To find the average of a list with Python, we can use the statistics.mean method.

For instance, we write:

import statistics

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

avg = statistics.mean(l)
print(avg)

We call statistics.mean with l to calculate the average of the entries in l and return it.

Therefore, avg is 20.11111111111111.

Conclusion

To find the average of a list with Python, we can use the statistics.mean method.

Categories
Python Answers

How to filter a dict to contain only certain keys with Python?

Sometimes, we want to filter a dict to contain only certain keys with Python.

In this article, we’ll look at how to filter a dict to contain only certain keys with Python.

How to filter a dict to contain only certain keys with Python?

To filter a dict to contain only certain keys with Python, we can use dictionary comprehension.

For instance, we write:

d = {'foo': 1, 'foobar': 2, 'bar': 3}

foodict = {k: v for k, v in d.items() if k.startswith('foo')}
print(foodict)

We have a dict d with a few keys.

And we want to create a dict that has the entries with keys that starts with 'foo'.

To do this, we loop through the items with for k, v in d.items().

And then we call k.startswith('foo') to return only the entries that starts with 'foo'.

Therefore, foodict is {'foo': 1, 'foobar': 2}.

Conclusion

To filter a dict to contain only certain keys with Python, we can use dictionary comprehension.

Categories
Python Answers

How to get an absolute file path in Python?

Sometimes, we want to get an absolute file path in Python.

In this article, we’ll look at how to get an absolute file path in Python.

How to get an absolute file path in Python?

To get an absolute file path in Python, we can use the os.path.abspath method.

For instance, we write:

import os

p = os.path.abspath("/foo.txt")
print(p)

We call os.path.abspath with the relative path to the file and returns the absolute path.

Then we get that p is something like '/foo.txt'.

Conclusion

To get an absolute file path in Python, we can use the os.path.abspath method.