使用 CSV 到数组函数丢失字符串中的空格
我正在使用一个将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎你可以使用 Tokenizer 类,或者一些已经存在的解析器。
当我执行你的函数时:
它按预期工作 - 我得到一个带空格的字符串。
您的逻辑仅适用于带引号的字符串:
如果您不在引号中,并且字符不是空格,则会添加它。
因此,当您不在引号中并遇到空格时,它不会附加到字符串中。
实际上,这可以通过 1 行 RegEx 来完成。
扩展 Taytay 的单行 CSV 解析器,下面是一个示例实现:
CsvParser.as
This seems like you could use a Tokenizer class, or some parser already in existence.
When I execute your function as:
It works as expected - I get a string with spaces.
Your logic only functions for quoted strings:
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