【发布时间】:2018-09-05 23:55:51
【问题描述】:
我刚刚发现,由于某种原因,使用 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 为我工作)。
【问题讨论】:
标签: python string utf-8 decode pyperclip