Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CsvUtil bug #49

Open
Object-wty opened this issue Dec 7, 2023 · 0 comments
Open

CsvUtil bug #49

Object-wty opened this issue Dec 7, 2023 · 0 comments

Comments

@Object-wty
Copy link

问题描述及重现代码:

在使用读取CSV文件中,我发现 对于 行 以逗号结尾,会导致最后一个空字段被忽略

对于此CSV文件
Data,Date,"Start Time","Device ID","Sequence Result","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBrG.RMS Level.Ch1","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBrG.RMS Level.Ch2","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBrG.Gain.Ch1","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBrG.Gain.Ch2","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBFS.RMS Level.Ch1","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBFS.RMS Level.Ch2","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBFS.Gain.Ch1","Left and Right Midrange Prospect 3/4 - Analog.Level and Gain 0dBFS.Gain.Ch2","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBrG.THD+N Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBrG.THD+N Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBrG.THD Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBrG.THD Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBrG.Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBrG.Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBFS.THD+N Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBFS.THD+N Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBFS.THD Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBFS.THD Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBFS.Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.THD+N 0dBFS.Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4 - Analog.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4 - Analog.Noise (RMS).Noise Level.Ch1","Left and Right Midrange Prospect 3/4 - Analog.Noise (RMS).Noise Level.Ch2","Woofer Prospect 1.Level and Gain 0dBrG.RMS Level.Ch1","Woofer Prospect 1.Level and Gain 0dBrG.Gain.Ch1","Woofer Prospect 1.Level and Gain 0dBFS.RMS Level.Ch1","Woofer Prospect 1.Level and Gain 0dBFS.Gain.Ch1","Woofer Prospect 1.THD+N 0dBrG.THD+N Ratio.Ch1","Woofer Prospect 1.THD+N 0dBrG.THD Ratio.Ch1","Woofer Prospect 1.THD+N 0dBrG.Noise Ratio.Ch1","Woofer Prospect 1.THD+N 0dBFS.THD+N Ratio.Ch1","Woofer Prospect 1.THD+N 0dBFS.THD Ratio.Ch1","Woofer Prospect 1.THD+N 0dBFS.Noise Ratio.Ch1","Woofer Prospect 1.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch1","Woofer Prospect 1.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch1","Woofer Prospect 1.Noise (RMS).Noise Level.Ch1","Left and Right Midrange Prospect 3/4.Level and Gain 0dBrG.RMS Level.Ch1","Left and Right Midrange Prospect 3/4.Level and Gain 0dBrG.RMS Level.Ch2","Left and Right Midrange Prospect 3/4.Level and Gain 0dBrG.Gain.Ch1","Left and Right Midrange Prospect 3/4.Level and Gain 0dBrG.Gain.Ch2","Left and Right Midrange Prospect 3/4.Level and Gain 0dBFS.RMS Level.Ch1","Left and Right Midrange Prospect 3/4.Level and Gain 0dBFS.RMS Level.Ch2","Left and Right Midrange Prospect 3/4.Level and Gain 0dBFS.Gain.Ch1","Left and Right Midrange Prospect 3/4.Level and Gain 0dBFS.Gain.Ch2","Left and Right Midrange Prospect 3/4.THD+N 0dBrG.THD+N Ratio.Ch1","Left and Right Midrange Prospect 3/4.THD+N 0dBrG.THD+N Ratio.Ch2","Left and Right Midrange Prospect 3/4.THD+N 0dBrG.THD Ratio.Ch1","Left and Right Midrange Prospect 3/4.THD+N 0dBrG.THD Ratio.Ch2","Left and Right Midrange Prospect 3/4.THD+N 0dBrG.Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4.THD+N 0dBrG.Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4.THD+N 0dBFS.THD+N Ratio.Ch1","Left and Right Midrange Prospect 3/4.THD+N 0dBFS.THD+N Ratio.Ch2","Left and Right Midrange Prospect 3/4.THD+N 0dBFS.THD Ratio.Ch1","Left and Right Midrange Prospect 3/4.THD+N 0dBFS.THD Ratio.Ch2","Left and Right Midrange Prospect 3/4.THD+N 0dBFS.Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4.THD+N 0dBFS.Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4.Noise (RMS).Noise Level.Ch1","Left and Right Midrange Prospect 3/4.Noise (RMS).Noise Level.Ch2","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBrG.RMS Level.Ch1","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBrG.RMS Level.Ch2","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBrG.Gain.Ch1","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBrG.Gain.Ch2","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBFS.RMS Level.Ch1","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBFS.RMS Level.Ch2","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBFS.Gain.Ch1","Left and Right Tweeter Prospect 2/5.Level and Gain 0dBFS.Gain.Ch2","Left and Right Tweeter Prospect 2/5.THD+N 0dBrG.THD+N Ratio.Ch1","Left and Right Tweeter Prospect 2/5.THD+N 0dBrG.THD+N Ratio.Ch2","Left and Right Tweeter Prospect 2/5.THD+N 0dBrG.THD Ratio.Ch1","Left and Right Tweeter Prospect 2/5.THD+N 0dBrG.THD Ratio.Ch2","Left and Right Tweeter Prospect 2/5.THD+N 0dBrG.Noise Ratio.Ch1","Left and Right Tweeter Prospect 2/5.THD+N 0dBrG.Noise Ratio.Ch2","Left and Right Tweeter Prospect 2/5.THD+N 0dBFS.THD+N Ratio.Ch1","Left and Right Tweeter Prospect 2/5.THD+N 0dBFS.THD+N Ratio.Ch2","Left and Right Tweeter Prospect 2/5.THD+N 0dBFS.THD Ratio.Ch1","Left and Right Tweeter Prospect 2/5.THD+N 0dBFS.THD Ratio.Ch2","Left and Right Tweeter Prospect 2/5.THD+N 0dBFS.Noise Ratio.Ch1","Left and Right Tweeter Prospect 2/5.THD+N 0dBFS.Noise Ratio.Ch2","Left and Right Tweeter Prospect 2/5.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch1","Left and Right Tweeter Prospect 2/5.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch2","Left and Right Tweeter Prospect 2/5.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch1","Left and Right Tweeter Prospect 2/5.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch2","Left and Right Tweeter Prospect 2/5.Noise (RMS).Noise Level.Ch1","Left and Right Tweeter Prospect 2/5.Noise (RMS).Noise Level.Ch2","Prospect Center Midrange 6.Level and Gain 0dBrG.RMS Level.Ch1","Prospect Center Midrange 6.Level and Gain 0dBrG.Gain.Ch1","Prospect Center Midrange 6.Level and Gain 0dBFS.RMS Level.Ch1","Prospect Center Midrange 6.Level and Gain 0dBFS.Gain.Ch1","Prospect Center Midrange 6.THD+N 0dBrG.THD+N Ratio.Ch1","Prospect Center Midrange 6.THD+N 0dBrG.THD Ratio.Ch1","Prospect Center Midrange 6.THD+N 0dBrG.Noise Ratio.Ch1","Prospect Center Midrange 6.THD+N 0dBFS.THD+N Ratio.Ch1","Prospect Center Midrange 6.THD+N 0dBFS.THD Ratio.Ch1","Prospect Center Midrange 6.THD+N 0dBFS.Noise Ratio.Ch1","Prospect Center Midrange 6.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch1","Prospect Center Midrange 6.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch1","Prospect Center Midrange 6.Noise (RMS).Noise Level.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBrG.RMS Level.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBrG.RMS Level.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBrG.Gain.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBrG.Gain.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBFS.RMS Level.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBFS.RMS Level.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBFS.Gain.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Level and Gain 0dBFS.Gain.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBrG.THD+N Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBrG.THD+N Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBrG.THD Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBrG.THD Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBrG.Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBrG.Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBFS.THD+N Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBFS.THD+N Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBFS.THD Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBFS.THD Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBFS.Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.THD+N 0dBFS.Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Signal to Noise Ratio 0dBrG.Signal to Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Signal to Noise Ratio 0dBFS.Signal to Noise Ratio.Ch2","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Noise (RMS).Noise Level.Ch1","Left and Right Midrange Prospect 3/4-HDMI_eARC by optical.Noise (RMS).Noise Level.Ch2"
"Upper Limit",,,,,21,21,,,22.5,22.5,,,0.5,0.5,,,,,0.5,0.5,,,,,,,,,0.001,0.001,29.75,,,,1,,,1,,,,,0.001,21,21,,,22.5,22.5,,,0.5,0.5,,,,,0.5,0.5,,,,,,,,,0.001,0.001,21,21,,,22.25,22.25,,,0.15,0.15,,,,,0.15,0.15,,,,,,,,,0.001,0.001,21.25,,22.5,,0.15,,,0.5,,,,,0.001,21,21,,,22.5,22.5,,,0.5,0.5,,,,,0.5,0.5,,,,,,,,,0.001,0.001
"Lower Limit",,,,,20.5,20.5,,,21.75,21.75,,,,,,,,,,,,,,,90,90,96,96,,,29.25,,32,,,,,,,,100,100,,20.5,20.5,,,21.75,21.75,,,,,,,,,,,,,,,100,100,100,100,,,20.5,20.5,,,21.25,21.25,,,,,,,,,,,,,,,93,93,96,96,,,20.25,,21.75,,,,,,,,100,100,,20.5,20.5,,,21.75,21.75,,,,,,,,,,,,,,,100,100,100,100,,
"Sequence Run",2023/10/28,10:21:54.34,"MAIPPZZ411064.1 000101 28082023",Passed,20.7553137337816,20.8085873598258,31.4117669954574,31.4650406215016,22.0903066859847,22.2058608637584,16.0697067727051,16.1852609504788,0.0422713080407445,0.0531463279186451,0.0419968625355388,0.0532891057779757,0.00635754479825057,0.00167625612091051,0.141097871707324,0.142356338781098,0.140878821601163,0.142373455652497,0.00565265767507592,0.00264355034455069,97.1768650920401,98.4255181997559,98.4967972544717,99.5056336781323,0.000170926378900295,0.000146479390033562,29.5485586193416,184.204248002366,32.3042059517438,41.2297191105537,0.355987725917318,0.356261474055918,0.00786429279249979,0.367403266248286,0.366792791760397,0.0106169436720765,107.294339344525,110.054389113204,0.000127276582776321,20.6338674926916,20.7346871314688,76.8013355686699,77.697984698885,22.1016502524687,22.2114839885997,12.737452899651,12.8995416141295,0.0397132231632423,0.05285857136185,0.0390019351845244,0.0527776279957878,0.00635349608043334,0.00152652399411142,0.141977319536209,0.142721667886812,0.141765847865571,0.142569524286504,0.00528949203576141,0.00126631762521586,102.932620924717,103.823493338095,104.229225621023,105.231294152819,0.00013701062818653,0.000105793173195675,20.6571900618087,20.6120960590271,42.9156659326807,42.6934406466046,21.4945236891693,21.4414170211191,11.8775334706294,11.8051342285003,0.0300734106869141,0.0273286636602449,0.0269601896087416,0.0254109962486996,0.012734541284041,0.00987002879662896,0.0397972253670273,0.0408374520734095,0.0232658543366373,0.0264298357711246,0.0320657930584661,0.0311640053994398,99.8362677152458,103.1311322072,100.642636242109,104.094938173226,0.000157492549711691,0.00010773743880851,20.7015416118171,31.1448943084619,22.0336624532018,12.6381411390096,0.0643359047850133,0.0642933309563358,0.00161734072758636,0.145378657039482,0.145198188322831,0.0013723968871896,101.75298345944,103.131148967294,0.000143680627271535,20.645912549114,20.7686875745758,76.90791280772,78.0027252949928,22.1090267207384,22.2346910027235,12.748274741616,12.9340527729255,0.0409068729087126,0.0546946352557786,0.0403186458466762,0.0546596314602093,0.00651505055940001,0.00143233547341371,0.14169416335768,0.14425567521249,0.141536810077813,0.14416643041394,0.00506868179635785,0.00131971631084919,102.98400087543,103.89673737361,104.535621812651,105.29308170067,0.000137101288323348,0.000106165894569243

第三行就会比其他三行少一位
将 ParseLine这个函数这样修改,这个bug就可以解决了

private static List<string> ParseLine(string line, char delimiter, char quote)
        {
            List<string> fields = new List<string>();
            int i = 0;
            while (i < line.Length)
            {
                if (line[i] == quote)
                {
                    int j = i + 1;
                    while (j < line.Length)
                    {
                        if (line[j] == quote)
                        {
                            if (j + 1 < line.Length && line[j + 1] == delimiter)
                            {
                                j++; // 跳过转义的分隔符
                            }
                            else
                            {
                                break; // 引号结束了字段的解析
                            }
                        }
                        j++;
                    }

                    if (j >= line.Length || line[j] != quote)
                    {
                        throw new ArgumentException("Invalid CSV format: mismatched quotes.");
                    }

                    // 将解析出的字段添加到列表中,通过 Unescape 方法去除转义字符
                    fields.Add(Unescape(line.Substring(i + 1, j - i - 1), delimiter, quote));
                    i = j + 1;
                }
                else
                {
                    int j = line.IndexOf(delimiter, i);
                    if (j < 0)
                    {
                        fields.Add(line.Substring(i));
                        i = line.Length;
                    }
                    else
                    {
                        fields.Add(line.Substring(i, j - i));
                        i = j + 1;
                    }
                }
            }
            // 如果行末尾是以分隔符结尾的,则添加一个空字段
            if (line.EndsWith(delimiter.ToString()))
            {
                fields.Add(string.Empty);
            }
            return fields;
        }
// c# code

具体版本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant