How do I remove redundant commas when writing a txt-file from a table?

조회 수: 12 (최근 30일)
I created a txt-file from a table (139x39) which contains some blanks. The data is separated by commas as delimiter. Unfortunately in the resulting file, commas also separate the blanks:
Raw Data,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
"Friday, Dec 05, 2019,17:00:28",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
191205.txt,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
95,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
16,16,16,1,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
CPS,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Time,7Li,31P,39K,43Ca,44Ca,45Sc,47Ti,55Mn,77ArCl,82Se,83Kr,85Rb,88Sr,89Y,90Zr,93Nb,133Cs,137Ba,139La,140Ce,141Pr,146Nd,147Sm,153Eu,159Tb,160Gd,163Dy,165Ho,166Er,169Tm,172Yb,175Lu,178Hf,181Ta,182W,208Pb,232Th,238U
0,0,1300,1900,0,200,0,0,400,0,750,700,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
%underneath this are another 100 rows with data in the same format as the previous row
Ideally, the content of the file should look like this - any idea how to do this?
Also if someone knows a trick on how to remove the double-quotes in line 2, that would be much appreciated.
Raw Data
Friday, Dec 05, 2019,17:00:28
191205.txt
95
0
16,16,16,1,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
CPS
Time,7Li,31P,39K,43Ca,44Ca,45Sc,47Ti,55Mn,77ArCl,82Se,83Kr,85Rb,88Sr,89Y,90Zr,93Nb,133Cs,137Ba,139La,140Ce,141Pr,146Nd,147Sm,153Eu,159Tb,160Gd,163Dy,165Ho,166Er,169Tm,172Yb,175Lu,178Hf,181Ta,182W,208Pb,232Th,238U
0,0,1300,1900,0,200,0,0,400,0,750,700,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

채택된 답변

Image Analyst
Image Analyst 2020년 1월 28일
You could read in the file and use strrep() to replace each ,, with , until there are no ,, left. Basically (untested)
fid = fopen(filename, 'rt');
fidOut = fopen(outputFileName, 'wt')
textLine = fgetl(fid);
while ischar(textLine)
while contains(textLine, ',,')
textLine = strrep(textLine, ',,', ',');
end
% Now write out
fprintf(fidOut, '%s\n', textLine);
% Now read in the next line.
textLine = fgetl(fid);
end
fclose(fid);
fclose(fidOut);
  댓글 수: 3
Jule
Jule 2020년 1월 28일
Ah, there was an accidental additional space in 'rt ' - it works with 'rt'.
This works except for the last comma in the row, thanks!!
Image Analyst
Image Analyst 2020년 1월 28일
You can get rid of trailing comma like this
if endsWith(textLine, ',')
% textLine ends with a comma. Strip it off.
textLine = textLine(1:end-1);
end
You might look at Stephen's code. It's more compact, though more cryptic if you don't know how to construct regular expressions.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2020년 1월 28일
편집: Stephen23 2020년 1월 28일
Regular expressions make this easy (removes all trailing commas, removes the double quotes, and leaves the newlines unchanged):
str = fileread('test.txt')
str = regexprep(str,{',+(\s*\n)','"([^\n"]+)"'},'$1')
[fid,msg] = fopen('test_new.txt','w');
assert(fid>=3,msg)
fprintf(fid,'%s',str)
fclose(fid);
Giving:
Raw Data
Friday, Dec 05, 2019,17:00:28
191205.txt
95
0
16,16,16,1,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
CPS
Time,7Li,31P,39K,43Ca,44Ca,45Sc,47Ti,55Mn,77ArCl,82Se,83Kr,85Rb,88Sr,89Y,90Zr,93Nb,133Cs,137Ba,139La,140Ce,141Pr,146Nd,147Sm,153Eu,159Tb,160Gd,163Dy,165Ho,166Er,1
69Tm,172Yb,175Lu,178Hf,181Ta,182W,208Pb,232Th,238U
0,0,1300,1900,0,200,0,0,400,0,750,700,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by