python - 如何从python中的剪贴板获取文本并粘贴到utf-8中?

标签 python

我的剪贴板中有一些 utf-8 文本和一个没有任何内容的 utf-8 .txt 文件。

如果我在 notepad++ 中打开该文件,并使用 ctrl+v 进行粘贴,我会得到 utf-8 格式的文件,其中不包含 ? 。

但是如果我使用 python 代码从剪贴板获取文本并以追加模式打开文件并将其写入其中,然后在 notepad++ 中查看该文件,我会看到其中有 ? 。

是否有一些特殊的Python代码可以从剪贴板获取文本并将其写入空的utf-8文件,这样如果我在notepad++中查看该文件,我就不会看到那些? .

有人知道吗?

最佳答案

使用编解码器模块:

 import codecs

 file = "/path/to/save/file.txt" 

 # instead of open(file,'w') do:
 f = codecs.open(file, encoding='utf-8',mode='w+')

 # Write a unicode string to the file.
 f.write(u'\u4500 blah blah blah\n')

关于python - 如何从python中的剪贴板获取文本并粘贴到utf-8中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10150349/

相关文章:

python - 使用正则表达式从Python文本中提取特定单词

python - 如何在 Python 中转义反斜杠和单引号或双引号?

python - Keras/Tensorflow : ValueError: Shape (? ,12) 等级必须为 1

python - 使用 txredisapi 建立连接后订阅和取消订阅 channel

使用子流程的 Python 自动化

python - PyDNS 和 DNS 超时错误

python - 如何生成具有预定义概率分布的随机数?

python - 为什么 Python 和 wc 在字节数上不一致?

python - 在我的 Django Digital Ocean 服务器上的什么位置设置环境变量?

python - 异步函数中的"RecursionError: maximum recursion depth exceeded in comparison"