如何使用中文文本执行此操作?

问题描述 投票:0回答:1

以下内容适用于英文歌词,但不适用于中文歌词。

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
。如何处理中文字符串?

python pyautogui cjk
1个回答
0
投票

问题出在您打开文件的方式上。尝试使用以下语法:

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")
© www.soinside.com 2019 - 2024. All rights reserved.