我刚刚发现,由于某些原因,当使用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对我有效)。
发布于 2018-09-06 08:18:54
您似乎遇到了一些使用非ASCII引号的问题。我建议你使用Python 3.7。下面是一个示例:
import pyperclip
with open('chat.txt', 'r') as f:
string = f.read()
pyperclip.copy(string)这是Python 2.7的替代方案:
import pyperclip
import sys
reload(sys)
sys.setdefaultencoding('utf8')
with open('chat.txt', 'r') as f:
string = f.read()
pyperclip.copy(string)警告:正如@lenz在评论中指出的那样,对于 .来说,使用是一种黑客行为,不被鼓励。
发布于 2018-09-24 15:59:59
此问题已在1.6.5中修复,因此您所要做的就是通过运行pip install -U pyperclip来更新pyperclip。
https://stackoverflow.com/questions/52194746
复制相似问题