列表中的變量與代碼一起工作正常:pyautogui.typewrite(i) 并在列表中途停止隨機工作。下面可以進行哪些改進,我怎樣才能不惜一切代價讓它到達文件的末尾?它可能與 for 循環以外的其他語句有關,還是我應該想出更好的方法來從文本文件中獲取列表?提前謝謝你的幫助。results = [] with open('H:\RetiredDevices.txt') as inputfile: for line in inputfile: results.append(line)while True: for i in results: pyautogui.click(PressEnter1) pyautogui.click(PressEnter1) time.sleep(1) pyautogui.click(PressEnter2) #pyautogui.click(PressEnter3) pyautogui.click(PressEnter4) pyautogui.typewrite(i) pyautogui.press('enter') time.sleep(1) retired_devices.append(i) results.remove(i) if len(results) == 0: break
2 回答

四季花海
TA貢獻1811條經驗 獲得超5個贊
這導致您跳過列表中的某些元素: results.remove(i)
演示:
res = [k for k in range(0,10)]
for i in res:
print(i)
res.remove(i)
>>>output
0
2
4
6
8

料青山看我應如是
TA貢獻1772條經驗 獲得超8個贊
不要results.remove(i)
在循環本身中使用。您通常不想在您仍在迭代的列表中添加或刪除正在迭代的列表中的內容。通常我會建議將所有要刪除的項目添加到另一個列表中,然后迭代該列表以將它們從原始列表中刪除。這看起來你已經在其加入retired_devices
,所以我會遍歷retired_devices
的循環結束后,并results.remove(i)
在每一個我retired_devices
。您也不需要while
循環,for
循環將在完成所有元素后結束。
添加回答
舉報
0/150
提交
取消