代码之家  ›  专栏  ›  技术社区  ›  Programer Beginner

Python pyperclip无法复制已解码的

  •  0
  • Programer Beginner  · 技术社区  · 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() 但后来发现,从那以后就行不通了 如果有类似 '


    编辑:替代解决方案

    除我接受的解决方案外,另一种解决方案是降低 pyperclip公司 1.6.4 )到较低版本( 1.6.1

    2 回复  |  直到 8 年前
        1
  •  0
  •   xilopaint    8 年前

    您似乎面临一些非ASCII引号的问题。我建议您使用python3.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在评论中指出 sys.setdefaultencoding() number of reasons .

        2
  •  1
  •   Al Sweigart mayur    8 年前

    这个问题已经在1.6.5中修复,所以您所要做的就是通过运行 pip install -U pyperclip .