Categories
Python Answers

How to replace all elements of Python NumPy Array that are greater than some value?

Sometimes, we want to replace all elements of Python NumPy Array that are greater than some value.

In this article, we’ll look at how to replace all elements of Python NumPy Array that are greater than some value.

How to replace all elements of Python NumPy Array that are greater than some value?

To replace all elements of Python NumPy Array that are greater than some value, we can get the values with the given condition and assign them to new values.

For instance, we write

import numpy as np

A = np.random.rand(500, 500)
A[A > 0.5] = 5

to create a NumPy array A with some random values.

Then we get all the values that are bigger than 0.5 and set them to 5 with

A[A > 0.5] = 5

Conclusion

To replace all elements of Python NumPy Array that are greater than some value, we can get the values with the given condition and assign them to new values.

Categories
Python Answers

How to send file using POST from a Python script?

Sometimes, we want to send file using POST from a Python script.

In this article, we’ll look at how to send file using POST from a Python script.

How to send file using POST from a Python script?

To send file using POST from a Python script, we call request.post with the files argument.

For instance, we write

with open('report.xls', 'rb') as f:
    r = requests.post('http://httpbin.org/post', files={'report.xls': f})

to open the report.xls file with open.

Then we call requests.post with the URL we want to send the file to via a POST request.

And we set the files argument to a dict with the key for the file and the file f itself.

Conclusion

To send file using POST from a Python script, we call request.post with the files argument.

Categories
Python Answers

How to percent-encode URL parameters in Python?

Sometimes, we want to percent-encode URL parameters in Python.

In this article, we’ll look at how to percent-encode URL parameters in Python.

How to percent-encode URL parameters in Python?

To percent-encode URL parameters in Python, we can use the urllib.parse module.

For instance, we write

import urllib.parse

print(urllib.parse.quote("Müller".encode('utf8')))

to call urllib.parse.quote with the "Müller".encode('utf8')) UTF string to percent encode the string.

We can decode the string with unquote by writing

print(urllib.parse.unquote("M%C3%BCller"))

Conclusion

To percent-encode URL parameters in Python, we can use the urllib.parse module.

Categories
Python Answers

How to get a value of datetime.today() in Python that is time zone aware?

Sometimes, we want to get a value of datetime.today() in Python that is time zone aware.

In this article, we’ll look at how to get a value of datetime.today() in Python that is time zone aware.

How to get a value of datetime.today() in Python that is time zone aware?

To get a value of datetime.today() in Python that is time zone aware, we can use the pytz module.

For instance, we write

import pytz
from datetime import datetime

utc_now = datetime.now(pytz.utc)

to call datetime.now with pytz.utc to get the current datetime in UTC.

Conclusion

To get a value of datetime.today() in Python that is time zone aware, we can use the pytz module.

Categories
Python Answers

How to choose longest string in list with Python?

Sometimes, we want to choose longest string in list with Python.

In this article, we’ll look at how to choose longest string in list with Python.

How to choose longest string in list with Python?

To choose longest string in list with Python, we can use the max function with the key argument set to len.

For instance, we write

my_list = ['123','123456','1234']
print(max(my_list, key=len))

to call max with my_list with key set to len to get the longest string from my_list.

Conclusion

To choose longest string in list with Python, we can use the max function with the key argument set to len.