Converting a text document to a CSV
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to convert a text document to a CSV file. The lines of the text document don't have consistent number of words or delimiters but have some common features. Like this:
Prologue Groove 15m Severe *. Alan Hill, Michael Barnard. 3 Sep 2016.
Variation: Chrome Nun Finish E2 5b **. Michael Barnard, Alan Hill. 10 Sep 2016.
They need to look like this:
Prologue Groove | 15m | Severe * | Alan Hill, Michael Barnard | 3 Sep 2016
Variation: Chrome Nun Finish | 22m | E2 5b ** | Michael Barnard, Alan Hill | 10 Sep 2016
Where there are full stops I am using strrep and it is working fine but am struggling with delimiters either side of the distance (always a number, 1, 2 or 3 digit, followed by m)
Any ideas???
Thanks
0 Kommentare
Antworten (1)
Stephen23
am 12 Sep. 2020
Bearbeitet: Stephen23
am 12 Sep. 2020
str = fileread('temp2.txt');
spl = regexp(str,'[\n\r]+','split');
spl = regexp(spl,'\s*\.\s*','split');
spl = vertcat(spl{:});
tmp = regexp(spl(:,1),'^(\D+?)\s+(\d+m)\s+(.+)','tokens','once');
tmp = [vertcat(tmp{:}),spl(:,2:3)].';
fmt = ['%s',repmat(' | %s',1,4),'\n'];
fid = fopen('temp3.csv','wt');
fprintf(fid,fmt,tmp{:});
fclose(fid);
Which prints this in the output file:
Prologue Groove | 15m | Severe * | Alan Hill, Michael Barnard | 3 Sep 2016
Variation: Chrome Nun Finish | 22m | E2 5b ** | Michael Barnard, Alan Hill | 10 Sep 2016
The test files are also attached (I added the "22m", which seems to be missing from your example).
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!