- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
最近,一位信誉良好的 SO 用户通知我,TStringList
存在拆分错误,这会导致它无法解析 CSV 数据。我还没有被告知这些错误的性质,并且在互联网上进行了搜索,包括 Quality Central没有产生任何结果,所以我才问。什么是TStringList 拆分错误?
请注意,我对毫无根据的意见答案不感兴趣。
我所知道的:
不多......其中之一是,这些错误很少在测试数据中出现,但在现实世界中并不罕见。
另一个是,如上所述,它们会阻止正确解析 CSV。考虑到很难用测试数据重现这些错误,我(可能)正在向尝试过在生产代码中使用字符串列表作为 CSV 解析器的人寻求帮助。
不相关的问题:
我获得了有关“Delphi-XE”标记问题的信息,因此由于“空格字符被视为分隔符”而导致解析失败feature请勿应用。因为StrictDelimiter
的介绍Delphi 2006 的属性解决了这个问题。我本人正在使用 Delphi 2007。
另外由于字符串列表只能容纳字符串,因此它只负责分割字段。由于区域设置差异等引起的涉及字段值(例如日期、 float ......)的任何转换困难不在范围内。
基本规则:
CSV 没有标准规范。但从 various specifications 推断出一些基本规则。
下面演示了 TStringList 如何处理这些。规则和示例字符串来自Wikipedia 。括号 ([
]
) 叠加在字符串周围,以便能够通过测试代码看到前导或尾随空格(如果相关)。
空格被视为字段的一部分,不应被忽略。
Test string: [1997, Ford , E350]Items: [1997] [ Ford ] [ E350]
Fields with embedded commas must be enclosed within double-quote characters.
Test string: [1997,Ford,E350,"Super, luxurious truck"]Items: [1997] [Ford] [E350] [Super, luxurious truck]
Fields with embedded double-quote characters must be enclosed within double-quote characters, and each of the embedded double-quote characters must be represented by a pair of double-quote characters.
Test string: [1997,Ford,E350,"Super, ""luxurious"" truck"]Items: [1997] [Ford] [E350] [Super, "luxurious" truck]
Fields with embedded line breaks must be enclosed within double-quote characters.
Test string: [1997,Ford,E350,"Go get one nowthey are going fast"]Items: [1997] [Ford] [E350] [Go get one nowthey are going fast]
In CSV implementations that trim leading or trailing spaces, fields with such spaces must be enclosed within double-quote characters.
Test string: [1997,Ford,E350," Super luxurious truck "]Items: [1997] [Ford] [E350] [ Super luxurious truck ]
Fields may always be enclosed within double-quote characters, whether necessary or not.
Test string: ["1997","Ford","E350"]Items: [1997] [Ford] [E350]
Testing code:
var
SL: TStringList;
rule: string;
function GetItemsText: string;
var
i: Integer;
begin
for i := 0 to SL.Count - 1 do
Result := Result + '[' + SL[i] + '] ';
end;
procedure Test(TestStr: string);
begin
SL.DelimitedText := TestStr;
Writeln(rule + sLineBreak, 'Test string: [', TestStr + ']' + sLineBreak,
'Items: ' + GetItemsText + sLineBreak);
end;
begin
SL := TStringList.Create;
SL.Delimiter := ','; // default, but ";" is used with some locales
SL.QuoteChar := '"'; // default
SL.StrictDelimiter := True; // required: strings are separated *only* by Delimiter
rule := 'Spaces are considered part of a field and should not be ignored.';
Test('1997, Ford , E350');
rule := 'Fields with embedded commas must be enclosed within double-quote characters.';
Test('1997,Ford,E350,"Super, luxurious truck"');
rule := 'Fields with embedded double-quote characters must be enclosed within double-quote characters, and each of the embedded double-quote characters must be represented by a pair of double-quote characters.';
Test('1997,Ford,E350,"Super, ""luxurious"" truck"');
rule := 'Fields with embedded line breaks must be enclosed within double-quote characters.';
Test('1997,Ford,E350,"Go get one now'#10#13'they are going fast"');
rule := 'In CSV implementations that trim leading or trailing spaces, fields with such spaces must be enclosed within double-quote characters.';
Test('1997,Ford,E350," Super luxurious truck "');
rule := 'Fields may always be enclosed within double-quote characters, whether necessary or not.';
Test('"1997","Ford","E350"');
SL.Free;
end;
如果您已阅读全部内容,那么问题是:),什么是“TStringList 拆分错误?”
最佳答案
Not much... One is that, these bugs show up rarely with test data, but not so rarely in real world.
只需要一个案例。测试数据不是随机数据,一个有一个失败案例的用户应该提交数据,然后我们就得到了一个测试案例。如果没有人可以提供测试数据,也许就没有错误/失败?
There's no standard specification for CSV.
这确实有助于消除困惑。如果没有标准规范,如何证明某些事情是错误的?如果仅靠自己的直觉,你可能会遇到各种各样的麻烦。以下是我自己与政府发行的软件愉快互动的一些内容;我的应用程序应该以 CSV 格式导出数据,而政府应用程序应该导入它。以下是我们连续几年陷入很多麻烦的原因:
Field,"",Field
无效,应该是Field,,Field
。我很有趣地向我的客户解释政府应用程序从一周到下一周更改了验证规则......0
,然后又强制不包含 0
。也就是说,在某个时间 Field,0,Field
有效,下一个 Field,,Field
是唯一有效的方式...这是另一个测试用例,(我的)直觉失败了:
1997, Ford, E350, "Super, luxurious truck"
请注意,
和"Super
之间的空格,以及"Super
后面的非常幸运的逗号。 TStrings
使用的解析器仅在紧跟在分隔符之后的情况下才能看到引号字符。该字符串被解析为:
[1997]
[ Ford]
[ E350]
[ "Super]
[ luxurious truck"]
直觉上我期望:
[1997]
[ Ford]
[ E350]
[Super luxurious truck]
但是你猜怎么着,Excel 的做法与 Delphi 的做法相同...
TStrings.CommaText
相当好并且实现得很好,至少我看过的 Delphi 2010 版本相当有效(避免了多个字符串分配,使用 PChar
来“walk”已解析的字符串),其工作方式与 Excel 的解析器大致相同。TStrings
。当它确实发生时,可能不是 TString
的错!关于delphi - TStringList 拆分错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6461140/
假设我有这个变量 var image = "image.jpg"; 我正在尝试拆分变量图像的内容并将 _thumbs 插入其中以获得类似 image_thumbs.jpg 的内容。 我该如何解决这个问
我有一个包含多个问题和答案的单元格,其组织方式类似于 CSV。因此,为了将所有这些问题和答案分开,使用逗号作为分隔符的简单拆分应该很容易分开。 不幸的是,有些值使用逗号作为小数分隔符。有没有办法避免这
这是简单的代码: import std.algorithm; import std.array; import std.file; void main(string[] args) { aut
我正在尝试解析一个看起来像的 txt 文件 A - 19 B - 2 C - 3 我正在使用扫描仪方法读取它并在“- ”中拆分,以便我的输出看起来像: A 19 B 2 C 3 但是它似乎没有正确拆分
我有这些网址字符串 file:///home/we/Pictures/neededWord/3193_n.jpg file:///home/smes/Pictures/neededWord/jds_2
我正在解析一个 CVS 文件,如下所示: "07555555555",25.70,18/11/2010,01/03/2011,N,133,0,36,,896,537,547,,Mr,John,Doe,
我在脚本中使用以下行返回 $folder 处所有文件夹的所有路径地点。 dir -recurse $folder|?{$_.PSIsContainer}|select -ExpandProperty
我正在尝试将字符串格式化为word+word+word 例如 “超音乐节”变成“超+音乐+节日” 我尝试过使用以下代码 query.split(" ").join("+"); 或 query.repl
我叫 luis,住在 arg。我有一个问题,无法解决。 **IN BASH** pwd /home/labs-perl ls file1.pl file2.pl **IN PERL** my $ls
我想从包 javax.json 中拆分 JsonArray,但我找不到完成这项工作的便捷方法。我查看了文档,只能想到迭代 JsonArray 并使用 JsonArrayBuilder 手动添加项目。
我希望在第一个 ':' 处拆分字符串,以防止字符串的第二部分包含 ':' 时出现问题。我一直在研究正则表达式,但仍然遇到一些问题,有人可以帮我吗?谢谢。 最佳答案 您可以使用overload of s
我想拆分列表的列表 ((A,1,2,3),(B,4,5,6),(C,7,8,9))进入: (A,1) (A,2) (A,3) (B,4) (B,5) ... 我试过rdd.flatMapValues(
我有一个文本文件,其中每一行都有数据。它看起来像这样: number0;text0 number1;text1 number2;text2 ..等等 所以我通过 xmlhttprequest 将该文本
问题很简单——比如说,我得到了函数,它接收数组作为参数 void calc(double[] data) 如何将这些数据“拆分”成两个子数组并像这样传递给子函数 calc_sub(data(0, le
我想显示来自 EMAIL_TEXT 数据库列的数据,在定义的字符处拆分列。出于某种原因,我的结果只打印第一行到我拆分字符串的位置,跳过其余行。这是我希望在每个“|”之后拆分的数据。 这里是要拆分的数据
我有一个动态数组,我想排除字符串的第一部分,但我不知道第一部分之后会有多少对象,我想将它们全部包含在一个新字符串中。 string = "text.'''hi''','''who''' '''are'
我想拆分 URL 的某些特定部分,这是我目前所做的。 var query = window.location.pathname.split( '/' ); query = window.locati
我有一条消息携带 XML(订单),其中包含多个同质节点(比如产品列表)以及其他信息(比如地址、客户详细信息等)。我必须使用另一个外部服务提供的详细信息来丰富每个“产品”,并返回带有丰富“产品”的相同完
我有一个动态生成的大字符串,我正在拆分它。 var myString="val1, val, val3, val4..... val400" 我对此字符串进行了简单的拆分, myString= myS
这个问题在这里已经有了答案: Java String split removed empty values (5 个答案) 关闭 7 年前。 我正在尝试使用 split(";") 将字符串转换为数组
我是一名优秀的程序员,十分优秀!