Categories
Python Answers

How to convert a string with dot and comma into a float in Python?

Sometimes, we want to convert a string with dot and comma into a float in Python.

In this article, we’ll look at how to convert a string with dot and comma into a float in Python.

How to convert a string with dot and comma into a float in Python?

To convert a string with dot and comma into a float in Python, we can remove the commas from the string before we call float.

For instance, we write

n = float("123,456.908".replace(',',''))

to call float with a string that has the commas removed from the "123,456.908" string with

"123,456.908".replace(',','')

Then we can convert the string to a float.

Conclusion

To convert a string with dot and comma into a float in Python, we can remove the commas from the string before we call float.

Categories
Python Answers

How to set color as a function of a third variable in a scatterplot with Python Matplotlib?

Sometimes, we want to set color as a function of a third variable in a scatterplot with Python Matplotlib.

In this article, we’ll look at how to set color as a function of a third variable in a scatterplot with Python Matplotlib.

How to set color as a function of a third variable in a scatterplot with Python Matplotlib?

To set color as a function of a third variable in a scatterplot with Python Matplotlib, we call scatter with the c argument.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt

x = np.random.random(10)
y = np.random.random(10)

plt.scatter(x, y, c=y, s=500)
plt.gray()

plt.show()

We generate data for the x and y axes with

x = np.random.random(10)
y = np.random.random(10)

Then we call scatter with x, y and c set to y to set the color to the y value.

Then we call gray to make the plot grayscale.

Finally, we call show to show the plot.

Conclusion

To set color as a function of a third variable in a scatterplot with Python Matplotlib, we call scatter with the c argument.

Categories
Python Answers

How to make Firefox headless programmatically in Selenium with Python?

Sometimes, we want to make Firefox headless programmatically in Selenium with Python.

In this article, we’ll look at how to make Firefox headless programmatically in Selenium with Python.

How to make Firefox headless programmatically in Selenium with Python?

To make Firefox headless programmatically in Selenium with Python, we can set the headless property to True.

For instance, we write

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://example.com/")
driver.quit()

to create an Options object.

And we set the headless property of it to True.

Then we create a webdriver.Firefox object with the options argument set to options to make Firefox headless.

Then we call get to open a web page at the given URL.

Conclusion

To make Firefox headless programmatically in Selenium with Python, we can set the headless property to True.

Categories
Python Answers

How to get real time output using subprocess with Python?

Sometimes, we want to get real time output using subprocess with Python.

In this article, we’ll look at how to get real time output using subprocess with Python.

How to get real time output using subprocess with Python?

To get real time output using subprocess with Python, we loop through the lines that are returned with the iterator that we get with stdout.readline.

For instance, we write

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1)
for line in iter(p.stdout.readline, b''):
    print(line)
p.stdout.close()
p.wait()

to call Popen with the cmd command string to run.

And we set stdout to subprocess.PIPE and bufsize to 1 to get the output.

Then we show the output with

for line in iter(p.stdout.readline, b''):
    print(line)

And then we call close to stop reading from stdout.

Conclusion

To get real time output using subprocess with Python, we loop through the lines that are returned with the iterator that we get with stdout.readline.

Categories
Python Answers

How to remove duplicates from a list of lists with Python?

Sometimes, we want to remove duplicates from a list of lists with Python.

In this article, we’ll look at how to remove duplicates from a list of lists with Python.

How to remove duplicates from a list of lists with Python?

To remove duplicates from a list of lists with Python, we can use the itertools.groupby method.

For instrance, we write

k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
import itertools

k.sort()
l = list(k for k, _ in itertools.groupby(k))

to call sort on k to sort it in place.

Then we call itertools.groupby(k) to create an iterator with tuples with the nested list k as the first item.

And then we extract that from each tuple with list comprehension and convert the returned iterator to a list with list.

Conclusion

To remove duplicates from a list of lists with Python, we can use the itertools.groupby method.