以下内容适用于英文歌词,但不适用于中文歌词。
import pyautogui, time
time.sleep(5)
f = open('/Users/a27/爱.txt', 'r','UTF-8')
for word in f:
pyautogui.typewrite(word)
pyautogui.press("enter")
它说
str object cannot be interpreted as an integer
。如何处理中文字符串?
问题出在您打开文件的方式上。尝试使用以下语法:
with open('/Users/a27/爱.txt', 'r', encoding='utf-8') as f:
import pyautogui, time
time.sleep(5)
with open('/Users/a27/爱.txt', 'r', encoding='utf-8') as f:
for word in f:
pyautogui.typewrite(word)
pyautogui.press("enter")