<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
h2
        {mso-style-priority:9;
        mso-style-link:"Heading 2 Char";
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:18.0pt;
        font-family:"Calibri",sans-serif;
        font-weight:bold;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.Heading2Char
        {mso-style-name:"Heading 2 Char";
        mso-style-priority:9;
        mso-style-link:"Heading 2";
        font-family:"Calibri Light",sans-serif;
        color:#2F5496;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Hi Antoine,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">I often have to read csv files with odd lines that trip functions like csvRead so I often use the method below.  It may solve your problem.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                dataread = mgetl(readfile);                                                         // Read everything<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                a = [];<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                b = [];<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                …<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                for i = 1: size(dataread, 'r') do<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                line = dataread(i);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                if length(line) ~= 0 then                                                 // Ignore blank lines<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                                line = tokens(line, [' ', ',', ascii(9)]);            // Accept spaces, commas or tabs<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                                if and(isnum(line)) then                                // If the line is all-numeric<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                                                line = strtod(line);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                                                a = [a; line(1)];<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                                                b = [b; line(2)];<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                                                …<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                                end<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                                end<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">                end<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0" width="600" style="width:450.0pt">
<tbody>
<tr>
<td width="600" style="width:450.0pt;padding:1.5pt 1.5pt 1.5pt 1.5pt">
<p class="MsoNormal" style="margin-bottom:12.0pt">Adrian Weeks <br>
Development Engineer, Hardware Engineering EMEA<br>
Office: +44 (0)2920 528500 | Desk: +44 (0)2920 528523 | Fax: +44 (0)2920 520178<br>
<a href="mailto:aweeks@hidglobal.com">aweeks@hidglobal.com</a><o:p></o:p></p>
</td>
</tr>
<tr>
<td width="600" style="width:450.0pt;padding:1.5pt 1.5pt 1.5pt 1.5pt">
<p class="MsoNormal"><a href="http://www.hidglobal.com/"><span style="text-decoration:none"><img border="0" width="234" height="36" style="width:2.4375in;height:.375in" id="Picture_x0020_1" src="cid:image001.gif@01D61CB7.E0474B90" alt="HID Global Logo"></span></a><o:p></o:p></p>
</td>
</tr>
<tr>
<td width="600" style="width:450.0pt;padding:1.5pt 1.5pt 1.5pt 1.5pt">
<p class="MsoNormal">Unit 3, Cae Gwyrdd,<br>
Green meadow Springs,<br>
Cardiff, UK,<br>
CF15 7AB.<br>
<a href="http://www.hidglobal.com">www.hidglobal.com</a><o:p></o:p></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> users <users-bounces@lists.scilab.org>
<b>On Behalf Of </b>Antoine Monmayrant<br>
<b>Sent:</b> 27 April 2020 16:41<br>
<b>To:</b> Users mailing list for Scilab <users@lists.scilab.org><br>
<b>Subject:</b> [EXT] [Scilab-users] parsing TSV (or CSV) file with scilab is a nightmare<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal" align="center" style="text-align:center;text-indent:12.0pt;background:#FFB3B3">
<b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">*** Please use caution this is an externally originating email. ***
</span></b><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif"><o:p></o:p></span></b></p>
</div>
<div>
<p>Hi all,<o:p></o:p></p>
<p><o:p> </o:p></p>
<p>This is both a rant and desperate cry for help.<br>
I'm trying to parse some TSV data (tab separated data file) with scilab and I cannot find a way to navigate around the minefield of bugs present in meof/mgetl/mgetstr/csvRead.<o:p></o:p></p>
<p>A bit of context: I need to load into scilab data generated by a closed source software.<br>
The data is in the form of many TSV files (that I cannot share in full, just some redacted bits) with a header and a footer.<br>
I don't want to hand modify these files or edit them in any way (I need to keep this as portable as possible, so no sed/awk/grep...)<o:p></o:p></p>
<h2>OPTION 1: csvRead<o:p></o:p></h2>
<p>That's the most intuitive solution, however, because of <a href="https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fbugzilla.scilab.org%2Fshow_bug.cgi%3Fid%3D16391&data=02%7C01%7Caweeks%40hidglobal.com%7C958a9bb7c76f40cef22108d7eac3d120%7Cf0bdc1c951484f86ac40edd976e1814c%7C0%7C0%7C637235999087304170&sdata=Myj7OkrpbGfSl3LD4QAoYzifF80drUrz6nPjP9H7xC8%3D&reserved=0">
http://bugzilla.scilab.org/show_bug.cgi?id=16391</a> and the presence of more than 1 empty line in my header/footer, this crashes Scilab.<o:p></o:p></p>
<h2>OPTION 2: hand parsing line by line using mgetl/meof<o:p></o:p></h2>
<p>I tried:<o:p></o:p></p>
<p>filename="tsv.txt";<br>
[fd, err] = mopen(filename, 'rt');<br>
while ~meof(fd) do<br>
    txtline=mgetl(fd,1);<br>
end<br>
mclose(fd)<o:p></o:p></p>
<p>Saddly, and contrary to what's written in "help mgetl", meof keeps on returning 0, well passed the end of the file and the while never ends!<o:p></o:p></p>
<h2>OPTION 3: hand parsing chunk by chunk using mgetstr/meof<o:p></o:p></h2>
<p>"help meof" does not confirm that meof should work with mgetl, but mgetstr is specifically listed.<br>
I thus tried:<o:p></o:p></p>
<p>filename="tsv.txt";<br>
[fd, err] = mopen(filename, 'rt');<br>
while ~meof(fd) do<br>
    txtchunk=mgetstr(80,fd);<br>
end<br>
mclose(fd)<o:p></o:p></p>
<p>But thanks to <a href="https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fbugzilla.scilab.org%2Fshow_bug.cgi%3Fid%3D16419&data=02%7C01%7Caweeks%40hidglobal.com%7C958a9bb7c76f40cef22108d7eac3d120%7Cf0bdc1c951484f86ac40edd976e1814c%7C0%7C0%7C637235999087304170&sdata=BOnmop38zy8wUtFRwrPSoVl9HTsJT6NcQAY23qPK8f8%3D&reserved=0">
http://bugzilla.scilab.org/show_bug.cgi?id=16419</a> this is also crashing Scilab.<o:p></o:p></p>
<p><o:p> </o:p></p>
<h2>OPTION 4: Can anyone here help me with this?<o:p></o:p></h2>
<p>I am really running out of ideas.<br>
Did I miss some -hmm- obvious combination of available file parsing scilab functions to achieve my goal?<br>
I have the feeling that it would have been faster for me to just learn a totally new language that does not suck at parsing files than trying to get it to work with scilab....<o:p></o:p></p>
<p><o:p> </o:p></p>
<p>Antoine<o:p></o:p></p>
<p>(depressed)<o:p></o:p></p>
<p><o:p> </o:p></p>
<p><o:p> </o:p></p>
<p><a href="https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fbugzilla.scilab.org%2Fshow_bug.cgi%3Fid%3D16419&data=02%7C01%7Caweeks%40hidglobal.com%7C958a9bb7c76f40cef22108d7eac3d120%7Cf0bdc1c951484f86ac40edd976e1814c%7C0%7C0%7C637235999087314165&sdata=hrLO7KWziAoacs9ytFJzziqv89FCY46SUNdZEUgvKNQ%3D&reserved=0">http://bugzilla.scilab.org/show_bug.cgi?id=16419</a><o:p></o:p></p>
</div>
</div>
</body>
</html>