根据 RFC 4180 验证 CSV 文件,规则 "The last field in the record must not be followed by a comma." 是否错误?
To validate CSV files according to RFC 4180, is the rule "The last field in the record must not be followed by a comma." wrong?
RFC 4180 在第 2 页指出:
Within the header and each record, there may be one or more fields, separated by commas. Each line should contain the same number of fields throughout the file. Spaces are considered part of a field and should not be ignored. The last field in the record must not be followed by a comma.
因此,根据此标准,这将是无效的:
cat,dog,cow,
不过理论上应该代表一行“猫”、“狗”、“牛”、“”。因此,如果添加逗号会创建一个新的“last”元素,则该规则实际上永远不会出错。事实上,要尊重“每一行在整个文件中应包含相同数量的字段”。在这种情况下我们需要它:
aaa,bbb,ccc,ddd
cat,dog,cow,
事实上,一些导出 CSV 的程序会执行此操作以填充(例如:Google 表格)。
综上所述,以下是尊重标准的唯一正确方式吗?
aaa,bbb,ccc,ddd
cat,dog,cow,""
或者规则是错误的还是多余的?我是不是理解错了?
这个规则没有错,但是一定要读的很文艺:最后一个字段后面不能跟逗号。
如果最后一个元素为空,则为 last-but-one 元素,后跟逗号,完全没问题。
这样就可以了
a,b,c,d
x,y,z,
u,v,,
w,,,
但这是错误的
a,b,c,d
x,y,z,
d,e,f,g,
编辑 来自讨论
a,b,c,d,
e,f,g,h,
i,j,k,l,
m,n,o,p,
也是禁止的,根据问题规则
RFC 4180 在第 2 页指出:
Within the header and each record, there may be one or more fields, separated by commas. Each line should contain the same number of fields throughout the file. Spaces are considered part of a field and should not be ignored. The last field in the record must not be followed by a comma.
因此,根据此标准,这将是无效的:
cat,dog,cow,
不过理论上应该代表一行“猫”、“狗”、“牛”、“”。因此,如果添加逗号会创建一个新的“last”元素,则该规则实际上永远不会出错。事实上,要尊重“每一行在整个文件中应包含相同数量的字段”。在这种情况下我们需要它:
aaa,bbb,ccc,ddd
cat,dog,cow,
事实上,一些导出 CSV 的程序会执行此操作以填充(例如:Google 表格)。
综上所述,以下是尊重标准的唯一正确方式吗?
aaa,bbb,ccc,ddd
cat,dog,cow,""
或者规则是错误的还是多余的?我是不是理解错了?
这个规则没有错,但是一定要读的很文艺:最后一个字段后面不能跟逗号。
如果最后一个元素为空,则为 last-but-one 元素,后跟逗号,完全没问题。
这样就可以了
a,b,c,d
x,y,z,
u,v,,
w,,,
但这是错误的
a,b,c,d
x,y,z,
d,e,f,g,
编辑 来自讨论
a,b,c,d,
e,f,g,h,
i,j,k,l,
m,n,o,p,
也是禁止的,根据问题规则