Categories
Python Answers

How to trim string whitespace with Python?

Sometimes, we want to trim string whitespace with Python.

In this article, we’ll look at how to trim string whitespace with Python.

How to trim string whitespace with Python?

To trim string whitespace with Python, we can use the string’s strip method to trim leading and trailing whitespace.

rstrip strips trailing whitespace only.

And lstrip strips leading whitespace only.

For instance, we write:

s = "  \t a string example\t  "
s1 = s.strip()
s2 = s.rstrip()
s3 = s.lstrip()
print(s1)
print(s2)
print(s3)

We call strip, rstrip and lstrip on s to strip leading and trailing whitespaces, trailing whitespaces, and leading whitespaces respectively.

And then we assign the returned string to s1, s2 and s3.

Therefore, we get:

a string example
     a string example
a string example     

from the print output.

Conclusion

To trim string whitespace with Python, we can use the string’s strip method to trim leading and trailing whitespace.

rstrip strips trailing whitespace only.

And lstrip strips leading whitespace only.

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 can use the requests module.

For instance, we write:

import requests

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

We open the file with open.

And we open the file with read permission with 'rb'.

Then we call requests.post to make a POST request.

And we set the files parameter to a dictionary containing the file to send the file as the request payload.

Conclusion

To send file using POST from a Python script, we can use the requests module.

Categories
Python Answers

How to split a string of space separated numbers into integers with Python?

Sometimes, we want to split a string of space separated numbers into integers with Python.

In this article, we’ll look at how to split a string of space separated numbers into integers with Python.

How to split a string of space separated numbers into integers with Python?

To split a string of space separated numbers into integers with Python, we can use the map and list functions and string’s split method.

For instance, we write:

n = list(map(int, "42 0".split()))
print(n)

We call split to split the string into a list.

Then we call map with int to map the list entries into integers.

Finally, we call list to convert the map into a list.

Therefore, n is [42, 0].

Conclusion

To split a string of space separated numbers into integers with Python, we can use the map and list functions and string’s split method.

Categories
Python Answers

How to extract the n-th elements from a list of tuples with Python?

Sometimes, we want to extract the n-th elements from a list of tuples with Python.

In this article, we’ll look at how to extract the n-th elements from a list of tuples with Python.

How to extract the n-th elements from a list of tuples with Python?

To extract the n-th elements from a list of tuples with Python, we can use list comprehension.

For instance, we write:

elements = [(1, 1, 1), (2, 3, 7), (3, 5, 10)]
n = 1
e = [x[n] for x in elements]
print(e)

We get the n-th element from each tuple and put them into a list with [x[n] for x in elements].

x is the tuple being retrieved.

Therefore, e is [1, 3, 5].

Conclusion

To extract the n-th elements from a list of tuples with Python, we can use list comprehension.

Categories
Python Answers

How to test whether a Python NumPy array contains a given row?

Sometimes, we want to test whether a Python NumPy array contains a given row.

In this article, we’ll look at how to test whether a Python NumPy array contains a given row.

How to test whether a Python NumPy array contains a given row?

To test whether a Python NumPy array contains a given row, we can convert the NumPy array to a list and then use the in operator to check whether the list is in the nested list.

For instance, we write:

import numpy as np

a = np.array([[1, 2], [10, 20], [100, 200]])
l = a.tolist()
print([1, 2] in l)
print([1, 200] in l)

We can np.array with a nested list to create an array.

Then we call a.tolist to convert the NumPy array back to a list.

Next, we use the in operator to check whether [1, 2] and [1, 200] is in l.

Therefore, print should print:

True
False

since [1, 2] is in l and [1, 200] isn’t.

Conclusion

To test whether a Python NumPy array contains a given row, we can convert the NumPy array to a list and then use the in operator to check whether the list is in the nested list.