在python中,我遇到了从剪贴板将文件编写为txt文件的问题。我使用pyperclip.paste()
从剪贴板获取数据并编写文件。但是当我在Python中使用.write
命令创建一个.txt
文件时,文本文件在行之间有很大的空格,我不明白为什么。我会分享我意思的截图。
这是我用过的密码。
import pyperclip
file = open("file.txt", "w")
print(pyperclip.paste())
file.write(pyperclip.paste())
file.close()
这是我的剪贴板的内容,我正在尝试创建一个文本文件。它有正确的制表符和新的行格式。FYI,这是我从quizlet“复制文本”中获取的使用selenium的内容,但是不管我如何复制文本(记事本,word),问题依然存在。
Extrinsic pathway cascade of clotting factors in blood that has escaped the vascular system to form a clot on the outside of the injured vessel
Common Pathway where intrinsic and extrinsic pathways converge
Initiation of clotting pathway tissue damage cause by factor 7a in extrinsic pathway
Thrombin Activation factor 5,8,11,13
Thrombin - activate platelets
- convert fibrinogen to fibrin
- activate factors and Protein C
Tissue factor initiates the extrinsic pathway when released in response to tissue damage
Calcium needed extrinsic 9-10, 10-2
intrinsic 11-9, 9-10, 10-2
当将我的剪贴板内容粘贴到记事本上的图像粘贴到记事本时,它是正确的格式。
VSCode终端图像 I也使用print(pyperclip.paste())
,并且文本输出是正常的。
由python编写的格式不正确的file.txt当我使用file.write(pyperclip.paste())
编写文件时,在行之间有一些不应该存在的空格。
怎么一回事?我如何解决这个问题,并保存正确的文本文件?
发布于 2022-01-05 10:50:11
我的猜测是,这些行的末尾有一个\r\n
,Windows将其作为一个新行来处理,也许pyperclip将其视为两行。
您可以通过使用“拆分行”拆分文本来解决这一问题,然后将其与每行的单个\n
一起粘贴:
"\n".join(pyperclip.paste().splitlines())
https://stackoverflow.com/questions/70589921
复制相似问题