Categories
Python Answers

How to get string objects instead of Unicode from JSON with Python?

Sometimes, we want to get string objects instead of Unicode from JSON with Python.

In this article, we’ll look at how to get string objects instead of Unicode from JSON with Python.

How to get string objects instead of Unicode from JSON with Python?

To get string objects instead of Unicode from JSON with Python, we can use PyYAML to parse the JSON instead of the json module.

This is because JSON is a subset of YAML.

To install it, we run:

pip install PyYAML

Then we can call the safe_load method to parse a JSON string into string objects:

import json
import yaml

list_org = ['a', 'b']
list_dump = json.dumps(list_org)
a = yaml.safe_load(list_dump)
print(a)

We convert the list_org list to JSON with json.dumps.

Then we call yaml.safe_load on list_dump to return list parsed from the list_dump JSON string.

Therefore, a is:

['a', 'b']

and both strings are regular strings.

Conclusion

To get string objects instead of Unicode from JSON with Python, we can use PyYAML to parse the JSON instead of the json module.

This is because JSON is a subset of YAML.

Categories
Python Answers

How to convert a PIL Image into a NumPy array with Python?

Sometimes, we want to convert a PIL Image into a NumPy array with Python.

In this article, we’ll look at how to convert a PIL Image into a NumPy array with Python.

How to convert a PIL Image into a NumPy array with Python?

To convert a PIL Image into a NumPy array with Python, we call the Image.open method from PIL.

Next, we call convert with 'L' to convert that to an image object that we can pass into numpy.array.

Then we call numpy.array with the returned image object to return a NumPy array.

For instance, we write:

import numpy
from PIL import Image

img = Image.open("art.png").convert("L")
imgarr = numpy.array(img)
print(imgarr)

to call Image.open with the path to the image file.

Then we call convert with 'L' to return image object that we pass into numpy.array to generate the array and return it,

Therefore, imgarr is something like:

[[29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]
 ...
 [29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]]

Conclusion

To convert a PIL Image into a NumPy array with Python, we call the Image.open method from PIL.

Next, we call convert with 'L' to convert that to an image object that we can pass into numpy.array.

Then we call numpy.array with the returned image object to return a NumPy array.

Categories
Python Answers

How to fix the ‘NumPy array is not JSON serializable’ issue with Python?

Sometimes, we want to fix the ‘NumPy array is not JSON serializable’ issue with Python.

In this article, we’ll look at how to fix the ‘NumPy array is not JSON serializable’ issue with Python.

How to fix the ‘NumPy array is not JSON serializable’ issue with Python?

To fix the ‘NumPy array is not JSON serializable’ issue with Python, we can use the json.dump method with the codecs.open method and the NumPy array’s tolist method.

For instance, we write:

import numpy as np
import codecs, json

a = np.arange(10).reshape(2, 5)
b = a.tolist()
file_path = "data.json"
json.dump(b,
          codecs.open(file_path, 'w', encoding='utf-8'),
          separators=(',', ':'),
          sort_keys=True,
          indent=2)

We create an array with numbers 0 to 9 with np.arange(10).

And we convert that to a 2×5 array with reshape(2, 5).

Next, we call a.tolist to convert the a NumPy array to a list.

Then we call json.dump with b, the file to write to, the separators to insert between entries, whether to sort keys, and the number of spaces to indent to generate the JSON file from the b list.

codecs.open opens the data.json for writing.

And so data.json has:

[
  [
    0,
    1,
    2,
    3,
    4
  ],
  [
    5,
    6,
    7,
    8,
    9
  ]
]

Conclusion

To fix the ‘NumPy array is not JSON serializable’ issue with Python, we can use the json.dump method with the codecs.open method and the NumPy array’s tolist method.

Categories
Python Answers

How to create a GUID or UUID in Python?

Sometimes, we want to create a GUID or UUID in Python.

In this article, we’ll look at how to create a GUID or UUID in Python.

How to create a GUID or UUID in Python?

To create a GUID or UUID in Python, we can use the uuid module.

For instance, we write:

import uuid

id = uuid.uuid4()
print(id)

We call uuid.uuid4 to return a v4 UUID as a string.

Therefore, id is something like 'c55b58d6-aa0d-498e-96be-433f99492cfb'.

Conclusion

To create a GUID or UUID in Python, we can use the uuid module.

Categories
Python Answers

How to delete a character from a string using Python?

Sometimes, we want to delete a character from a string using Python.

In this article, we’ll look at how to delete a character from a string using Python.

How to delete a character from a string using Python?

To delete a character from a string using Python, we can use the string’s replace method.

For instance, we write:

oldstr = 'EXAMPLE'
newstr = oldstr.replace("M", "")
print(newstr)

We call oldstr.replace with 'M' and an empty string to remove ‘M’ from 'EXAMPLE' and return the new string

Therefore, newstr is 'EXAPLE'.

Conclusion

To delete a character from a string using Python, we can use the string’s replace method.