如何使用 pickle 保存加载数据并在另一个脚本中重新使用锚点数据

How to save load data with pickle and the re use the anchor data in another script

我一直在编写一个使用 pyautogui 自动点击屏幕的 snapchat 机器人。我使用锚点来保存鼠标点,我想使用 pickle 将锚点数据保存到一个文件中,以便在另一个每天运行一次的脚本中再次使用它。这是第一个的代码

import pyautogui as pag
import sys, json, requests, keyboard, time
import pickle

anchorNames = ["Camera Button", "Picture button", "Pick", "Group Button", "Select Button", "Send Button"]
anchors = []


anchorsFulfilled = 0
anchorsRequired  = len(anchorNames)

print(":: Click enter when your mouse is over '{0}' ::".format(anchorNames[anchorsFulfilled]))

while anchorsFulfilled != anchorsRequired:
    if keyboard.read_key() == "enter":

        mousePositionNow = pag.position()
        anchors.append(mousePositionNow)
        print("Successfully captured mouse coordinates.\n")
        anchorsFulfilled += 1
        if anchorsFulfilled == anchorsRequired: break
        print(":: Click enter when your mouse is over '{0}' ::".format(anchorNames[anchorsFulfilled]))
        time.sleep(1)

print("Cords captured: {0}\n".format(anchors))
print("Go Back To Picture Page And Press Enter to Start")



def sender():
    global anchors1
    cameraButton, pictureButton, pick, groupButton, selectbutton, sendButton = anchors

    pag.moveTo(pictureButton)
    pag.click(pictureButton)
    time.sleep(2)

    pag.moveTo(pick)
    pag.click(pick)
    time.sleep(1)

    pag.moveTo(groupButton)
    pag.click(groupButton)
    time.sleep(1)

    pag.moveTo(selectbutton)
    pag.click(selectbutton)
    time.sleep(1)

    pag.moveTo(sendButton)
    pag.click(sendButton)
    time.sleep(1)

    pag.moveTo(cameraButton)
    pag.click(cameraButton)
    time.sleep(0.5)

    with open("redo.txt", 'wb') as fp:
        pickle.dump(anchors, fp)


time.sleep(1)
if keyboard.read_key() == "enter":
    sender()

如您所见,它保存到 redo.txt 文件以备后用。 但是当我尝试将第二个脚本中的文本文件附加到锚点数据时,出现此错误。

cameraButton, pictureButton, pick, groupButton, selectbutton, sendButton = anchors ValueError: not enough values to unpack (expected 6, got 1)

这是第二个脚本的代码

import time
import pickle
import pyautogui as pag



anchors = []
pickle_in = open("redo.txt", "rb")
example_dict = pickle.load(pickle_in)
with (open("redo.txt", "rb")) as openfile:
    while True:
        try:
            anchors.append(pickle.load(openfile))
        except EOFError:
            break

def redo():
    global anchors
    cameraButton, pictureButton, pick, groupButton, selectbutton, sendButton = anchors

    pag.moveTo(pictureButton)
    pag.click(pictureButton)
    time.sleep(2)

    pag.moveTo(pick)
    pag.click(pick)
    time.sleep(1)

    pag.moveTo(groupButton)
    pag.click(groupButton)
    time.sleep(1)

    pag.moveTo(selectbutton)
    pag.click(selectbutton)
    time.sleep(1)

    pag.moveTo(sendButton)
    pag.click(sendButton)
    time.sleep(1)




time.sleep(3)
redo()

如何修复此错误并将锚点数据导入第二个脚本中的锚点?

正如@Toothless204 在评论中提到的那样,您正在 pickle anchors 列表并使用 append 将其加载回另一个列表。因此,第二个脚本中的变量 anchors 是一个包含原始 anchors 列表的列表。换句话说,它是一个列表列表。亲自尝试一下:

import pyautogui as pg
import pickle
import time 

anchors = []

pos1 = pg.position()
anchors.append(pos1)
print('Move mouse to a new location')
time.sleep(3)
pos2 = pg.position()
anchors.append(pos2)

with open('savefile.txt','wb') as fp:
     pickle.dump(anchors,fp)
 
print('anchors = ' , anchors)

anc1 = [] 
# append the anchors list to the list anc1, which results in a list of lists 
with open('savefile.txt','rb') as openfile:
    try:
      anc1.append(pickle.load(openfile))
    except:
      print('File read error')
print("anc1 = ", anc1)# anc1 is a list whose first and only element is the original anchors list
t1,t2 =anc1[0] # note that we use anc1[0] instead of anc1
print('t1 =', t1)
print('t2 =', t2)

# load the anchors list into anc2
with open('savefile.txt','rb') as openfile:
    try:
      anc2 = pickle.load(openfile)
    except:
      print('File read error')
print("anc2 = ", anc2)
t3,t4 = anc2
print('t3 = ',t3)
print('t4 =', t4)

anc3 = [] 
# extend list anc3 with the elements of the original anchors list
with open('savefile.txt','rb') as openfile:
    try:
      anc3.extend(pickle.load(openfile))
    except:
      print('File read error')
print("anc3 = ", anc3)
t5,t6 =anc3
print('t5 =', t5)
print('t6 =', t6)

这是一个示例输出:

anchors =  [Point(x=510, y=741), Point(x=1571, y=210)]                                                                  
anc1 =  [[Point(x=510, y=741), Point(x=1571, y=210)]]                                                                   
t1 = Point(x=510, y=741)                                                                                                
t2 = Point(x=1571, y=210)                                                                                               
anc2 =  [Point(x=510, y=741), Point(x=1571, y=210)]                                                                     
t3 =  Point(x=510, y=741)                                                                                               
t4 = Point(x=1571, y=210)                                                                                               
anc3 =  [Point(x=510, y=741), Point(x=1571, y=210)]                                                                     
t5 = Point(x=510, y=741)                                                                                                
t6 = Point(x=1571, y=210)