Converting a text document to a CSV

6 次查看(过去 30 天)
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

回答(1 个)

Stephen23
Stephen23 2020-9-12
编辑:Stephen23 2020-9-12
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).

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by