删除并加载pickle文件python

时间:2016-05-17 09:46:39

标签: python load pickle

对于学生项目,我必须从433MHz接收器录制样本并将其发回以替换发送它的初始按钮。

我成功接收并将其保存在pickle文件中。

但是现在,我不知道怎么把它送回来...... 我尝试了不同的东西,但没有任何工作。

请你帮我做一下吗?

以下是我的代码:

收到部分:

from __future__ import with_statement
from datetime import datetime
import matplotlib.pyplot as pyplot
import RPi.GPIO as GPIO
import pickle

RECEIVED_SIGNAL = [[], []]  #[[time of reading], [signal reading]]
MAX_DURATION = 5
RECEIVE_PIN = 23

outputb = open('button_on.pkl', 'w')
outputa = open('time_on.pkl' ,'w')
if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(RECEIVE_PIN, GPIO.IN)
    cumulative_time = 0
    beginning_time = datetime.now()
    print '**Started recording**'
    while cumulative_time < MAX_DURATION:
        time_delta = datetime.now() - beginning_time
        RECEIVED_SIGNAL[0].append(time_delta)
        RECEIVED_SIGNAL[1].append(GPIO.input(RECEIVE_PIN))
        cumulative_time = time_delta.seconds
    print '**Ended recording**'
    print len(RECEIVED_SIGNAL[0]), 'samples recorded'
    GPIO.cleanup()

    print '**Processing results**'
    for i in range(len(RECEIVED_SIGNAL[0])):
        RECEIVED_SIGNAL[0][i] = RECEIVED_SIGNAL[0][i].seconds + RECEIVED_SIGNAL[0][i].microseconds/1000000.0
        print RECEIVED_SIGNAL[0][i] , RECEIVED_SIGNAL[1][i]
        A= RECEIVED_SIGNAL[0][i]
        B= RECEIVED_SIGNAL[1][i]
        outputa.write(str(A))
        outputb.write(str(B))

    print '**Plotting results**'
    pyplot.plot(RECEIVED_SIGNAL[0], RECEIVED_SIGNAL[1])
    pyplot.axis([0, MAX_DURATION, -1, 2])
    pyplot.show()

    outputa.close()
    outputb.close()

传输部分:

import time
import sys
import RPi.GPIO as GPIO
import pprint
import pickle

button_on = "button_on.pkl"

with open(button_on, "rb") as f:
    print pickle.load(f)
button_on.close()

我有一个错误:

Traceback (most recent call last): 
print pickle.load(f) 
File '/usr/lib/python2.7/pickle.py', line 1378, in load 
return Unpickler(file).load() 
File '/usr/lib/python2.7/pickle.py', line 858, in load 
dispatch[key](self) 
File '/usr/lib/python2.7/pickle.py', line 1138, in load_pop del self.stack[.1] 
IndexError: list assignment index out of range

0 个答案:

没有答案