从一个文件内容的副本传给raw\u input()的字符串中奇怪地消失了CR

2024-04-20 16:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

为了弄清楚这个bug的原因,我最终遇到了Python 2.7中raw\u input()函数的奇怪行为:

它只从手动复制(通过剪贴板)文件内容的字符串中删除CR成对字符CR LF。传递给raw_input()的字符串是与前一个字符串相同的显示副本的字符串不会丢失其CR字符。 在所有的病例中,单独的CR碳保持不变。ACR(回车符)是一个\r字符。在

为了比混乱的描述更清楚,这里有一个代码描述了必须做什么来观察这个事实,它的命令只需要执行。在

重点在文本对象中:它有7个字符,而不是传递给raw\u input()来创建文本的8个字符。在

为了验证传递给raw_input()的参数是否真的有8个字符,我创建了另一个文件粘贴.txt用相同的参数。要确定这个问题中的某些内容确实是一项棘手的任务,正如在记事本+窗口中的复制所示:在这样一个窗口中,各种行尾(\r\n、\r\n)都显示为crlf。在

建议使用Ctrl-A选择文件的全部数据。在

我很困惑,想知道我是在编码或理解上犯了错误,还是这是Python的一个真正的特性。在

我希望你的评论和光。在

with open('PRIM.txt','wb') as f:
    f.write('A\rB\nC\r\nD')
print "  1) A file with name 'PRIM.txt' has just been created with content A\\rB\\nC\\r\\nD"
raw_input("  Open this file and copy manually its CONTENT in the clipboard.\n"+\
          "    --when done, press Enter to continue-- ")


print "\n  2) Paste this CONTENT in a Notepad++ window "+\
      "     and see the symbols at the extremities of the lines."
raw_input("    --when done, press Enter to continue-- ")


Text = raw_input("\n  3) Paste this CONTENT here and press a key : ")
print ("     An object Text has just been created with this pasted value of CONTENT.")


with open('PASTED.txt','wb') as f:
    f.write('')
print "\n  4) An empty file 'PASTED.txt' has just been created."
print "     Paste manually in this file the PRIM's CONTENT and shut this file."
raw_input("     --when done, press Enter to continue-- ")


print "\n  5) Enter the copy of this display of A\\rB\\nC\\r\\nD : \nA\rB\nC\r\nD"
DSP = raw_input('please, enter it on the following line :\n')
print "    An object DSP has just been created with this pasted value of this copied display"


print '\n----------'
with open('PRIM.txt','rb') as fv:
    verif = fv.read()
print "The read content of the file 'PRIM.txt' obtained by open() and read() : "+repr(verif)
print "len of the read content of the file 'PRIM.txt'  ==",len(verif)


print '\n----------'
print "The file PASTED.txt received by pasting the manually copied CONTENT of PRIM.txt"
with open('PASTED.txt','rb') as f:
    cpd = f.read()
    print "The read content of the file 'PASTED.txt' obtained by open() and read() "+\
          "is now : "+repr(cpd)
    print "its len is==",len(cpd)


print '\n----------'
print 'The object Text received through raw_input() the manually copied CONTENT of PRIM.txt'
print "value of Text=="+repr(Text)+\
      "\nText.split('\\r\\n')==",Text.split('\r\n')
print 'len of Text==',len(Text)


print '\n----------'
print "The object DSP received  through raw_input() the copy of the display of A\\rB\\nC\\r\\nD" 
print "value of DSP==",repr(DSP)
print 'len of DSP==',len(DSP)

我的操作系统是Windows。我想知道在其他操作系统上是否也有同样的情况。在


Tags: ofthetexttxtreadinputrawlen
2条回答

在我的文章之后,我可以从我的代码中查找,我确实注意到,从一个文件复制并传递给raw_input()的数据的修改与Python直接读取文件中的数据时所执行的新行修改是一样的,这一点在这里得到了证明:

with open("TestWindows.txt", 'wb') as f:
    f.write("PACIFIC \r  ARCTIC \n  ATLANTIC \r\n  ")

print "\n- Following string have been written in TestWindows.txt in mode 'wb' :\n"+\
      "PACIFIC \\r  ARCTIC \\n  ATLANTIC \\r\\n  "


print "\n- data got by reading the file TestWindows.txt in 'rb' mode :"
with open("TestWindows.txt", 'rb') as f:
    print "    repr(data)==",repr(f.read())

print "\n- data got by reading the file TestWindows.txt in 'r' mode :"
with open("TestWindows.txt", 'r') as f:
    print "    repr(data)==",repr(f.read())

print "\n- data got by reading the file TestWindows.txt in 'rU' mode :"
with open("TestWindows.txt", 'rU') as f:
    print "    repr(data)==",repr(f.read())

结果:

^{pr2}$

首先,文件粘贴.txt与文件的内容相同原稿.txt,由复制产生原稿.txt的内容并粘贴到粘贴.txt不在Python字符串中转换。所以,当数据通过剪贴板从一个文件传递到另一个文件时,它不会被修改。这一事实证明的内容原稿.txt完好无损地放在复制程序放入数据的剪贴板中。在

其次,从文件到Python字符串的数据通过clipboard和raw_input()被修改;因此修改发生在剪贴板和Python字符串之间。因此,我认为raw_input()可能会对从剪贴板接收到的数据执行与Python解释器从文件读取中接收数据相同的解释。在

然后,我又补充了这样一个观点:用\r\n替换\n是因为“Windows nature”的数据变成了“Python性质”的数据,而剪贴板不会引入数据修改,因为它是Windows操作系统控制下的一部分。在

唉,从屏幕复制数据并传递到raw_input()的事实并没有改变换行符\r\n,尽管这些数据是通过Windows的剪贴板传输的,这打破了我的小概念。在

然后我认为Python知道数据的性质不是因为它的源,而是因为数据中包含的信息;这种信息是一种“格式”。我发现了下面关于Windows剪贴板的页面,确实有几种格式可以用来记录剪贴板上的信息:

http://msdn.microsoft.com/en-us/library/ms648709(v=vs.85).aspx

也许,Python对\r\n修改的解释与剪贴板中存在的这些格式有关,也可能没有。但我对这些乱七八糟的事情还不够了解,我很难确定。在

有人能解释以上所有的观察结果吗?在

一。在

一。在

谢谢你的回答,恩寇兰。但我不认为这是原因:

  • 标准输入没有属性模式

  • 标准输入指键盘,就我所知。然而,在我的代码中,数据不是来自键盘上的键入,而是来自通过剪贴板的粘贴。这是不同的。

关键的一点是,我不明白Python interpeter如何区分来自剪贴板的数据是从文件复制的,还是来自剪贴板的数据是从屏幕上复制的

sys.stdin以文本模式打开(您可以通过显示sys.stdin.mode并查看它是'r'来检查这一点)。如果在Python中以文本模式打开任何文件,那么平台本机行尾(\r\n对于Windows)将转换为Python字符串中的一个简单换行符(\n)。在

通过使用模式'r'而不是'rb'打开PASTED.txt文件,可以在操作中看到这一点。在

相关问题 更多 >