How to parse a file with value pairs

조회 수: 4 (최근 30일)
Ruben Ruiloba
Ruben Ruiloba 2020년 8월 25일
편집: Stephen23 2020년 8월 25일
Hi was looking for ideas on how to best parse a text file which lines are in the following format
!FIX.BODY=xxxxx|2125=aaaaaa|1067=bbbbb|150=ccccc|329=ddddd|
the line is delimited by a '|' and it represents a value pair with the column name followed by an equal sign = followed by the value.
So in the example above I would like to end up with a table like this
FIX.BODY 2125 1067 150 329
___________________________________________
xxxxx aaaaaa bbbbb ccccc ddddd
  댓글 수: 3
Ruben Ruiloba
Ruben Ruiloba 2020년 8월 25일
Hi thanks for the reply. Yes the file has multiple lines and the lines don't all have the same number of variable names.
The lines can differ. I can't upload the file as it has confidential data but it would look something like this.
!FIX.BODY=xxxxxx|2125=aaaaaa|1067=bbbbb|150=ccccc|329=dddddd|
!FIX.BODY=xxxxxx|2125=akcklsd|150=cchscc|329=ddddsd|
!FIX.BODY=xxxxxx|2125=ajfalkjfa|4555=ndlsuel|908=akncld|123=hdeudnc|
Thanks
Stephen23
Stephen23 2020년 8월 25일
편집: Stephen23 2020년 8월 25일
"I can't upload the file as it has confidential data but it would look something like this."
A sample file does not have to contain confidential data in it, because you can write it with random, invented, made up data. However it should include all of the salient features of the actual data files, such as EOL character/s, file encoding (especially if this file is being generated from some other application), representative character strings, number encodings, etc.
Providing a file gives us an agreed reference with which we can test our code.
Not providing a file slows down you getting the solution you want.

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

채택된 답변

Stephen23
Stephen23 2020년 8월 25일
편집: Stephen23 2020년 8월 25일
This works with the attached file (which I had to create myself):
T = table();
[fid,msg] = fopen('temp0.txt','rt');
assert(fid>=3,msg)
while ~feof(fid)
str = fgetl(fid);
spl = regexp(str,'[^!|=]+','match');
vnm = genvarname(spl(1:2:end)); % GENVARNAME is not required for R2019b or later
T{end+1,vnm} = spl(2:2:end); %#ok<SAGROW>
end
fclose(fid);
Giving:
>> T
T =
FIX0x2EBODY x2125 x1067 x150 x329 x4555 x908 x123
___________ ___________ _______ ________ ________ _________ ________ _________
'xxxxxx' 'aaaaaa' 'bbbbb' 'ccccc' 'dddddd' [] [] []
'xxxxxx' 'akcklsd' [] 'cchscc' 'ddddsd' [] [] []
'xxxxxx' 'ajfalkjfa' [] [] [] 'ndlsuel' 'akncld' 'hdeudnc'
  댓글 수: 1
Ruben Ruiloba
Ruben Ruiloba 2020년 8월 25일
Thanks this has given me ideas and exactly what I was looking for. Thanks again.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by