Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

Trouble loading numpy array where they show pickle data error in python

I can successfully save & load small arrays using Numpy. Now I am saving the below array using np.save('array.npy')

[0, 100, 0, 5, 10, 15, 20, 25, 30, 25, 20, 15, 10, 5, 0]

when I try to load using np.load('array.npy'), it shows the below error:

    raise ValueError("Cannot load file containing pickled data "
ValueError: Cannot load file containing pickled data when allow_pickle=False

If I try to solve it by adding allow_pickle=True then it shows the below error:

    raise IOError(
OSError: Failed to interpret file 'array.npy' as a pickle

Its really a difficult situation. Please advise! :(

The code I am referring to is below:

def recv():
    import socket
    import time
    import numpy as np

    TCP_IP = "0.0.0.0"
    BUFFER_SIZE = 20  # Normally 1024, but we want fast response

    # receiving CAN frame payload
    TCP_PORT = 5003
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind((TCP_IP,TCP_PORT))
    s.listen(1)
    conn,addr = s.accept()
    while 1:
        data1 = conn.recv(BUFFER_SIZE)
        if not data1: break
        datalist = list(data1)
        print("CAN payload: %s" % datalist)
        conn.send(data1)  # echo
    conn.close()
    time.sleep(2)
    # ------------------------------------------------------------
    # assembling CAN frame
    from can import Message
    can_msg = Message(is_extended_id=bool(datalist[0]),arbitration_id=datalist[1],data=datalist[2:])

    # printing all received payloads
    print("CAN frame: ",can_msg)
    print("Vehicle speed: ",datalist[2:])

    # Saving all received payloads
    np.save('array.npy',datalist[2:])  # save


def EPS_process():
    # EPS process for Right turn, high speed
    import numpy as np

    print("Starting EPS process")
    speed_array = np.load('array.npy')  # load

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay, I want to share what resolves the situation. So basically @hpaulj was suggesting that for multiple processes the read might start before the save is finished. So I added a 3-sec delay before speed_array = np.load('array.npy')

This gives enough time for the NumPy array to save before I can read again. Otherwise, it will read the previously saved array and hence the pickle error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...