如何检测用户的语言环境以获得正确的csv分隔符? [英] How do I detect the user's locale to get the correct csv separator?

查看:102
本文介绍了如何检测用户的语言环境以获得正确的csv分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的数据转换工具,它可以产生的输出之一是一个csv文件.

I have a simple data conversion tool and one of the outputs it can produce is a csv file.

这在英国非常适合,但是当我将其运送给德国客户时,我遇到了一些问题.具体来说,它们使用','表示浮点数中的小数点,反之亦然.这意味着,当他们在excel中打开数据文件时,至少可以说:-)

This works perfectly here in the UK but when I shipped it out to a German customer I had some issues. Specifally, they use a ',' to represent the decimal point in a floating point number and vice versa. This means that when they open their data file in excel, the result is rather messy to say the least :-)

替换正确的字符是微不足道的,但是我如何检测是否要应用此字符?

Substituting the correct character is trivial, but how can I detect whether or not to apply this?

所以这个:

a,b,c
1.1,1.2,1.3
"1.1",1,2,"1,3"
"this,is,multi-
-line",this should be column 2, row 4
a;b;c
"a;b","c"

..加载到英国的excel中时看起来像这样:

..looks like this when loaded into excel in the UK:

+----------------+-----+-----+-----+
| a              | b   | c   |     |
+----------------+-----+-----+-----+
| 1.1            | 1.2 | 1.3 |     |
+----------------+-----+-----+-----+
| 1.1            | 1   | 2   | 1,3 |
+----------------+-----+-----+-----+
| this,is,multi- |     |     |     |
| -line          | 2   | 4   |     |
+----------------+-----+-----+-----+
| a;b;c          |     |     |     |
+----------------+-----+-----+-----+
| a;b            | c   |     |     |
+----------------+-----+-----+-----+

..但是在德国会发生什么?

..but what happens in Germany?

推荐答案

顾名思义,CSV文件应以逗号分隔,并且与本地无关.但是,可以避免此问题的方法是在CSV文件中将相关的十进制数字双引号,例如:"10,20", "1,50", "This is another column".这样可以完全避免出现任何问题的CSV解析器(例如 FileHelpers 库)将此读为10,201,50,而不是:1020150.

CSV files as the name suggest should be comma-seperated and are not local dependant. However what you could do to avoid this issue is double-quote the relevant decimal numbers within the CSV file as such: "10,20", "1,50", "This is another column". This should avoid the issue entirely for any decent CSV-parser (such as the FileHelpers library) which will read this as 10,20 and 1,50 and not as: 10, 20, 1, and 50.

请参见 CSV :

更复杂的CSV实现允许逗号和其他特殊字符 字段值中的字符.许多实现使用(双引号) 包含保留字符的值周围的字符(例如 逗号,双引号或换行符);嵌入式双引号字符 可以用一对连续的双引号表示

More sophisticated CSV implementations permit commas and other special characters in a field value. Many implementations use " (double quote) characters around values that contain reserved characters (such as commas, double quotes, or newlines); embedded double quote characters may be represented by a pair of consecutive double quotes

这篇关于如何检测用户的语言环境以获得正确的csv分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆