我刚刚发现,由于某种原因,在使用 pyperclip 复制已解码的字符串(使用 utf-8)时,它会引发错误。
import pyperclip
with open('chat.txt' 'r') as f:
string = f.read()
# the string is encoded in utf-8 in order to be able to write down `'`, `emoji` and other special signs or symbol
pyperclip.copy(string.decode('utf-8'))
它将引发此错误:PyperclipException: only str, int, float, and bool values can be copied to the clipboard, not unicode
我找到了一种迂回的方法来解决它,str()
但后来发现它不起作用,因为str()
如果有一些像'
.
编辑:替代解决方案
除了我接受的解决方案之外,另一种解决方案是将pyperclip从最新版本(现在它1.6.4
)降级到较低版本(1.6.1
对我有用)。