如何解决如何使用pickle保存加载数据并在另一个脚本中重新使用锚数据
我一直在编写一个使用 pyautogui 自动点击屏幕的 snapchat 机器人。我使用锚点来保存鼠标点,我想使用泡菜将锚点数据保存到一个文件中,以便在另一个每天运行一次的脚本中再次使用它。这是第一个的代码
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: 没有足够的值来解包(预期 6,得到 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,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 在评论中提到的,您正在酸洗 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=210)]]
t1 = Point(x=510,y=741)
t2 = Point(x=1571,y=210)
anc2 = [Point(x=510,y=210)]
t3 = Point(x=510,y=741)
t4 = Point(x=1571,y=210)
anc3 = [Point(x=510,y=210)]
t5 = Point(x=510,y=741)
t6 = Point(x=1571,y=210)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。