Categories
Python Answers

How to print string to a text file with Python?

Sometimes, we want to print string to a text file with Python.

In this article, we’ll look at how to print string to a text file with Python.

How to print string to a text file with Python?

To print string to a text file with Python, we can use the open and write methods.

For instance, we write:

with open("output.txt", "w") as text_file:
    text_file.write("foo")

We open the file we want to write to with a with statement and the open function.

We pass in the path of the text file and the permission as arguments of open.

In the block, we call text_file.write to write the string into the text file.

text_file is the file handle for the text file.

And the file will be closed automatically once the write operation is done since we used the with statement.

Conclusion

To print string to a text file with Python, we can use the open and write methods.

Categories
Python Answers

How to print the full Python NumPy array without truncation?

Sometimes, we want to print the full Python NumPy array without truncation.

In this article, we’ll look at how to print the full Python NumPy array without truncation.

How to print the full Python NumPy array without truncation?

To print the full Python NumPy array without truncation, we can use the numpy.setoptions method.

For instance, we write:

import sys
import numpy

numpy.set_printoptions(threshold=sys.maxsize)
l = numpy.arange(100)
print(l)

to set the print options to print everything with:

numpy.set_printoptions(threshold=sys.maxsize)

Then we define an array with evenly spaced list from 0 to 100 with `arange.

Therefore, l is:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99]

Conclusion

To print the full Python NumPy array without truncation, we can use the numpy.setoptions method.

Categories
Python Answers

How to crop an image in OpenCV using Python?

Sometimes, we want to crop an image in OpenCV using Python.

In this article, we’ll look at how to crop an image in OpenCV using Python.

How to crop an image in OpenCV using Python?

To crop an image in OpenCV using Python, we can call cv.imread to read the image file.

Then we take a slice of the image file object with the square bracket notation.

And then we call cv2.imshow to display the image in a window.

For instance, we write:

import cv2

img = cv2.imread("test1.png")
crop_img = img[0:100, 0:150]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

We call cv2.imread with the path string for the image and assign it to img.

Then we crop the image by passing the start and end pixels of the y-axis, and start and end pixels of the x-axis respectively.

Both numbers are in pixels.

Next, we call cv2.imshow with the crop_img image to display the cropped image in a window.

Conclusion

To crop an image in OpenCV using Python, we can call cv.imread to read the image file.

Then we take a slice of the image file object with the square bracket notation.

And then we call cv2.imshow to display the image in a window.

Categories
Python Answers

How to get a list of all subdirectories in the current directory with Python?

Sometimes, we want to get a list of all subdirectories in the current directory with Python.

In this article, we’ll look at how to get a list of all subdirectories in the current directory with Python.

How to get a list of all subdirectories in the current directory with Python?

To get a list of all subdirectories in the current directory with Python, we can use the os.walk method.

For instance, we write:

import os

directory = '/'
dirs = [x[0] for x in os.walk(directory)]
print(dirs)

We call os.walk with the directory path to return an iterator with the tuples containing the directory path strings.

Then we can get them get the directory path string from each tuple with x[0].

Therefore, dirs is something like ['./', './.upm'].

Conclusion

To get a list of all subdirectories in the current directory with Python, we can use the os.walk method.

Categories
Python Answers

How to JSON serialize a Decimal object with Python?

Sometimes, we want to JSON serialize a Decimal object with Python.

In this article, we’ll look at how to JSON serialize a Decimal object with Python.

How to JSON serialize a Decimal object with Python?

To JSON serialize a Decimal object with Python, we can use the json.dumps method from the simplejson module with use_decimal set to True.

To install it, we run:

pip install simplejson 

For instance, we write:

import simplejson as json
from decimal import Decimal

s = json.dumps(Decimal('3.9'), use_decimal=True)
print(s)

We use the Decimal constructor to create a decimal number object.

Then we call json.dumps from the simplejson module to convert the decimal number object to a number string.

Conclusion

To JSON serialize a Decimal object with Python, we can use the json.dumps method from the simplejson module with use_decimal set to True.