使用 CSV 到数组函数丢失字符串中的空格

发布于 2024-12-11 17:32:01 字数 2275 浏览 5 评论 0原文

我正在使用一个将 csv 数据转换为多维数组的函数。然后,我将遍历这些字符,以查找数组中处理字符串的方式的特定情况。一个例子是,如果我有一个类似“这是一个字符串,是的”的字符串,那么我确保不要计算字符串中的逗号,因为它位于该字符串的引号之间。无论如何,在下面的函数中,我有一些如何在结果中丢失空格的情况。我得到的不是“这是 AS3”,而是“thisisAS3”。空格似乎只在带引号的字符串中可用。有人知道这部分代码的问题出在哪里吗?

        function CSVtoArray(csv:String):Array {
        var inQuotes:Boolean = false;
        var field:String = "";
        var finalData:Array = new Array();
        finalData.push(new Array());
        var line:int = 0;
        //iterate each character
        for(var i:int = 0; i < csv.length; i++) {
            var c:String = csv.charAt(i);
            var n:String = csv.charAt(i+1);
            var ad:Boolean = false;  
            //if the quote repeats, add the character
            if(inQuotes && c == "\"" && n == "\"") {
                field += c; 
            }            
            //if we are inside quotes, add the character
            if(inQuotes && c != "\"") {
                field += c;    
            }     
            //if we are not inside quotes...
            if(!inQuotes && c != "\"") {
                //if this character is a comma, start a new field
                if(c == ",") {
                    finalData[line].push(field);
                    field = "";   
                //if this character is a newline, start a new line
                } else if(c == "\n") {
                    finalData[line].push(field);
                    finalData.push(new Array());
                    line++;
                    field = "";     
                //if this is not leading or trailing white space, add the character
                } else if(c != " " && c != "\t" && c != "\r") {
                    field += c;
                }            
            }      
            //if this is a quote, switch inQuotes
            if(c == "\"") {
                inQuotes = !inQuotes;
            }      
        }      
        //add last line
        finalData[line].push(field);
        //if the last line does not have the same length as the first, remove it
        if(finalData[line].length < finalData[0].length) finalData.pop();



        //return the resulting array
        return finalData;

    }

感谢您对此的任何帮助,非常感谢!

I am working with a function that converts csv data to a multi-dimentional array. I am then going through the characters to find specific situations in the way the strings are handled in the array. One example is if I have a string like - "this is a string, yeah" - then I make sure not to count the comma in the string because it is between quotes from that string. Anyhow In the following function I have some how lost my spaces in the results. Instead of getting "this is AS3" I am getting "thisisAS3". Spaces seem to only be available in the strings that have quotes. Anyone have an idea of where the issue in this portion of code is?

        function CSVtoArray(csv:String):Array {
        var inQuotes:Boolean = false;
        var field:String = "";
        var finalData:Array = new Array();
        finalData.push(new Array());
        var line:int = 0;
        //iterate each character
        for(var i:int = 0; i < csv.length; i++) {
            var c:String = csv.charAt(i);
            var n:String = csv.charAt(i+1);
            var ad:Boolean = false;  
            //if the quote repeats, add the character
            if(inQuotes && c == "\"" && n == "\"") {
                field += c; 
            }            
            //if we are inside quotes, add the character
            if(inQuotes && c != "\"") {
                field += c;    
            }     
            //if we are not inside quotes...
            if(!inQuotes && c != "\"") {
                //if this character is a comma, start a new field
                if(c == ",") {
                    finalData[line].push(field);
                    field = "";   
                //if this character is a newline, start a new line
                } else if(c == "\n") {
                    finalData[line].push(field);
                    finalData.push(new Array());
                    line++;
                    field = "";     
                //if this is not leading or trailing white space, add the character
                } else if(c != " " && c != "\t" && c != "\r") {
                    field += c;
                }            
            }      
            //if this is a quote, switch inQuotes
            if(c == "\"") {
                inQuotes = !inQuotes;
            }      
        }      
        //add last line
        finalData[line].push(field);
        //if the last line does not have the same length as the first, remove it
        if(finalData[line].length < finalData[0].length) finalData.pop();



        //return the resulting array
        return finalData;

    }

Thanks for any help on this it is much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-12-18 17:32:01

这似乎你可以使用 Tokenizer 类,或者一些已经存在的解析器。

当我执行你的函数时:

var result:String = CSVtoArray("\"this is a string, yeah\"");

它按预期工作 - 我得到一个带空格的字符串。

您的逻辑仅适用于带引号的字符串:

//if we are not inside quotes...
if(!inQuotes && c != "\"") {
// ...
    //if this is not leading or trailing white space, add the character
    } else if(c != " " && c != "\t" && c != "\r") {
        field += c;

如果您不在引号中,并且字符不是空格,则会添加它。

因此,当您不在引号中并遇到空格时,它不会附加到字符串中。

实际上,这可以通过 1 行 RegEx 来完成。

扩展 Taytay 的单行 CSV 解析器,下面是一个示例实现:

CsvParser.as

package
{
    import flash.display.Sprite;

    public class CsvParser extends Sprite
    {
        public function CsvParser()
        {
            var set1:Array = CSVtoArray("\"this is a string, yeah\"\n");
            var set2:Array = CSVtoArray("this is a string, yeah\n");
        }

        public function CSVtoArray(csv:String):Array
        {
            // split csv in to rows
            var rows:Array = csv.split("\n");

            // for every row...
            for (var x:uint = 0; x < rows.length; x++)
            {
                var columns:Array = csv.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/g);

                for (var y:uint = 0; y < columns.length; y++)
                {
                    // trim leading / trailing whitespace
                    columns[y] = columns[y].replace(/^\s+|\s+$/g, '');
                }

                rows[x] = columns;
            }

            return (rows);
        }

    }
}

This seems like you could use a Tokenizer class, or some parser already in existence.

When I execute your function as:

var result:String = CSVtoArray("\"this is a string, yeah\"");

It works as expected - I get a string with spaces.

Your logic only functions for quoted strings:

//if we are not inside quotes...
if(!inQuotes && c != "\"") {
// ...
    //if this is not leading or trailing white space, add the character
    } else if(c != " " && c != "\t" && c != "\r") {
        field += c;

If you are not in quotes, and the character is not whitespace is it added.

So, when you are not in quotes and encounter whitespace is it not appended to the string.

Actually, this can be accomplished with 1-line of RegEx.

Expanding on Taytay's one-line CSV parser, here's an example implementation:

CsvParser.as

package
{
    import flash.display.Sprite;

    public class CsvParser extends Sprite
    {
        public function CsvParser()
        {
            var set1:Array = CSVtoArray("\"this is a string, yeah\"\n");
            var set2:Array = CSVtoArray("this is a string, yeah\n");
        }

        public function CSVtoArray(csv:String):Array
        {
            // split csv in to rows
            var rows:Array = csv.split("\n");

            // for every row...
            for (var x:uint = 0; x < rows.length; x++)
            {
                var columns:Array = csv.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/g);

                for (var y:uint = 0; y < columns.length; y++)
                {
                    // trim leading / trailing whitespace
                    columns[y] = columns[y].replace(/^\s+|\s+$/g, '');
                }

                rows[x] = columns;
            }

            return (rows);
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文