I am trying to save dictionaries to a stream so that it can be loaded up later. The issue is that when I print the dictionary with a numpy array inside, it prints with the string "array" in front of the numpy array. Example:
import numpy as np
import pprint
a = {"hi":"greeting","celeryman":np.array([4,3,3])}
print(a)
pprint.pprint(a)
The above produces the output
{'celeryman': array([4, 3, 3]), 'hi': 'greeting'}
{'celeryman': array([4, 3, 3]), 'hi': 'greeting'}
The desired output is
{'celeryman': [4, 3, 3], 'hi': 'greeting'}
which I am hoping can be read without issue by using json.load.
Do I have to write my own printer for this?
you will have to assign the numpy array as list by-
a = {"hi":"greeting","celeryman":list(np.array([4,3,3]))}
and if you again want to initialise the list attached as value to the key 'celeryman' as a numpy array then-
a=json.load('yourfile.json')
newnplist=np.array(a['celeryman'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.