有时间我们会遇到如下的文件:
figure; I1=imread('1.jpg'); imshow(I1); hold on; Loc1=[y,y]; plot(Loc1,'r*'); % plot(Y) creates a 2-D line plot of the data in Y versus the indey of each value. % If Y is a matriy, then the plot function plots the columns of Y versus their row number. % The y-ayis scale ranges from 1 to the number of rows in Y
这个文件含有很多空行,这实在是一个不太好看的文件。你希望看到的文件是不是这样的:
figure; I1=imread('1.jpg'); imshow(I1); hold on; Loc1=[y,y]; plot(Loc1,'r*'); % plot(Y) creates a 2-D line plot of the data in Y versus the indey of each value. % If Y is a matriy, then the plot function plots the columns of Y versus their row number. % The y-ayis scale ranges from 1 to the number of rows in Y
如果我想去除这些空行干怎么办啦,手动删除?文件短小,手动来也快,大了怎么搞? 这里就用Matlab程序来搞定这个事情,其他程序语言应该也行只是好不好实现罢了,估计Python干这个比较简洁。不多说直接上解决的程序:
function RemoveFileEmptyRow(ReadFilePath,WriteFilePath) %% 2017/06/22 by DQ % 此算法为移除文本中空行 FidRead=fopen(ReadFilePath,'rb','ieee-le','GBK'); FidWrite=fopen(WriteFilePath,'wb','ieee-le','GBK'); while ~feof(FidRead) FileRowStr = fgetl(FidRead); if ~isempty(FileRowStr ) fprintf(FidWrite,'%s\n',FileRowStr); end end fclose(FidRead); fclose(FidWrite); end
好了把含有空行的文件命名为f1.txt,整理后的文件命名为f2.txt,然后运行一下上面这个程序,你就会得到想要的结果了。
clc; clear; close all; ReadFilePath='f1.txt'; WriteFilePath='f2.txt'; RemoveFileEmptyRow(ReadFilePath,WriteFilePath)
上面得到的结果保存在f2.txt,去这个文件看看是不是下面的内容:
figure; I1=imread('1.jpg'); imshow(I1); hold on; Loc1=[y,y]; plot(Loc1,'r*'); % plot(Y) creates a 2-D line plot of the data in Y versus the indey of each value. % If Y is a matriy, then the plot function plots the columns of Y versus their row number. % The y-ayis scale ranges from 1 to the number of rows in Y当很多文件出现这种状况,这种处理方式还是很便捷的把