我有一个分隔文件,如果它出现两次,需要删除最后一行。
file1 = open("File1.txt","r")
sent = file1.readlines()
sent = str(sent).lower().split()
u=0
ofile = open("Encoded.txt","r")
decode = ofile.readlines()
decode = str(decode).split()
u = 0
decode(0)==sent(0)<=== #Error here
while u < len(decode):
u = u+1
decode(u) == sent(u)<== #Error here
如果它返回“nothing”,那么我知道有一个额外的空白行,然后它将从文件中删除最后一行。
由于文件大小(最多2GB),有时我会收到“系统内存不足”错误。
是否还有其他方法可以检查文件末尾是否有空白行?
答案 0 :(得分:1)
分别读取每一行,跟踪当前行和上一行。当您到达文件末尾时,请检查它们的值。
Dim reader = File.OpenText(fileDirectory)
Dim line as String = ""
Din line2 as String = ""
Dim line3 as String = ""
Do
line3 = line2
line2 = line
line = reader.ReadLine
Loop Until line Is Nothing
reader.Close()
If String.IsNullOrEmpty(line2) And String.IsNullOrEmpty(line3) Then
' Do what you need to do
End If