Categories
Python Answers

How to extract a floating number from a string with Python?

Sometimes, we want to extract a floating number from a string with Python.

In this article, we’ll look at how to extract a floating number from a string with Python.

How to extract a floating number from a string with Python?

To extract a floating number from a string with Python, we call the re.findall method.

For instance, we write

import re

d = re.findall("\d+\.\d+", "Current Level: 13.4db.")

to call re.findall with a regex string to get the decimal numbers from the string.

Then d is a list of numbers found in the string in the 2nd argument.

Conclusion

To extract a floating number from a string with Python, we call the re.findall method.

Categories
Python Answers

How to create dictionary of separate variables with Python?

Sometimes, we want to create dictionary of separate variables with Python.

In this article, we’ll look at how to create dictionary of separate variables with Python.

How to create dictionary of separate variables with Python?

To create dictionary of separate variables with Python, we can get the local variables in a dict with locals.

For instance, we write

blah = 1
blah_name = [k for k, v in locals().items() if v is blah][0]

to get an iterator of tuples of the local variables with

k for k, v in locals().items()

Then we get the one from the iterator that has value blah with

if v is blah

And then we use index 0 to get the first entry that matches the condition.

Conclusion

To create dictionary of separate variables with Python, we can get the local variables in a dict with locals.

Categories
Python Answers

How to test if a string contains one of the substrings in a list, in Python Pandas?

Sometimes, we want to test if a string contains one of the substrings in a list, in Python Pandas.

In this article, we’ll look at how to test if a string contains one of the substrings in a list, in Python Pandas.

How to test if a string contains one of the substrings in a list, in Python Pandas?

To test if a string contains one of the substrings in a list, in Python Pandas, we can call str.contains.

For instance, we erite

searchfor = ['og', 'at']
r = s[s.str.contains('|'.join(searchfor))]

to call s.str.contains with '|'.join(searchfor) to search for strings listed in searchfor in our series s.

Conclusion

To test if a string contains one of the substrings in a list, in Python Pandas, we can call str.contains.

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 itertools module’s izip function.

For instance, we write

from itertools import izip

i = iter(a)
b = dict(izip(i, i))

to call iter to convert list a into an iterator.

Then we call izip with i for both arguments to zip the even and odd index entries together.

And then we call dict to convert the iterator to a dict.

Conclusion

To convert [key1,val1,key2,val2] to a dict with Python, we can use the itertools module’s izip function.

Categories
Python Answers

How to retrieve the output of subprocess.call() with Python?

Sometimes, we want to retrieve the output of subprocess.call() with Python.

In this article, we’ll look at how to retrieve the output of subprocess.call() with Python.

How to retrieve the output of subprocess.call() with Python?

To retrieve the output of subprocess.call() with Python, we call subprocess.check_output.

For instance, we write

import subprocess

print(subprocess.check_output(["ping", "-c", "1", "8.8.8.8"]))

to call subprocess.check_output with a list that has the command and command arguments.

And then we get the output from the returned string.

Conclusion

To retrieve the output of subprocess.call() with Python, we call subprocess.check_output.