Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这些字符串的计算结果不相等?_Python_String_Vim - Fatal编程技术网

Python 为什么这些字符串的计算结果不相等?

Python 为什么这些字符串的计算结果不相等?,python,string,vim,Python,String,Vim,下面是vimscript x.vim: python << endpy import vim import time import os cb = vim.current.buffer bufferString = '\n'.join(cb[:]) with open(cb.name, "r") as currentFile: fileString = currentFile.read() print bufferString print "=================

下面是vimscript x.vim:

python << endpy
import vim
import time
import os
cb = vim.current.buffer
bufferString = '\n'.join(cb[:])
with open(cb.name, "r") as currentFile:
    fileString = currentFile.read()
print bufferString
print "================="
print fileString
if bufferString != fileString:
    print "File changed!"
else:
    print "Nothing changed!"
endpy

python您读取的文件可能在最后一行末尾有一个换行符。但是,从
vim
获取行并将它们与
\n
连接在一起不会在最后一行的末尾有一个新行。

如果文件是在vim中编辑的(这似乎很可能),它几乎肯定会在末尾有一个新行,因为vim默认情况下会在那里放置一行。是的,这似乎是答案。尝试将
+'#'
添加到两行
打印
中。或者打印出
len(fileString)
len(bufferString)