Burning .csv file - regardless of culture

In German, langauage-decimal seperators are, and the values ​​of seperators are ";", in English / other languages ​​decimal separators. and delimiters of the values ​​",".

I want to create a .csv file without changing the current culture. I mean, always. The CSV file must have a "." has decimal separators and "," has value separators.

The code for this is below.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Globalization; namespace CSV_FILE_FORMAT { class Program { static void Main(string[] args) { string aFileName = "result.csv"; FileStream aFileStream = new FileStream(aFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); StreamWriter m_StreamWriter = new StreamWriter(aFileStream); double[] values = new double[] { 10.5, 20.3, 30.2 }; for(int i = 0; i < values.Length;i++) { m_StreamWriter.Write(values[i].ToString(CultureInfo.InvariantCulture)); m_StreamWriter.Write(","); } } } } 

The problem with this code is that the OS is in German. Decimal separators are shown by "," instead of ".".

Please let me know that the code is missing.

+6
source share
3 answers

You can probably get the decimal separator and list separator from the current culture.

 CultureInfo culture = new CultureInfo(currentCultureName); string decimalSeparator = culture.NumberFormat.NumberDecimalSeparator; string listSeparator = culture.TextInfo.ListSeparator; 

And, if the value being written contains any of the delimiters, enclose it in double quotation marks.

 string val = values[i].ToString(CultureInfo.InvariantCulture); if(val.Contains(decimalSeparator) || val.Contains(listSeparator)) { val = string.Format("\"{0}\"", val); } m_StreamWriter.Write(val); m_StreamWriter.Write(listSeparator); 

The name of the current culture may be something like this: "en-US" for the United States of America.

Hope this helps!

+6
source

A quick fix, so the problem of German culture is to insert these double values ​​with quotes. Results:

 English: "10.5","20.3","30.2" German: "10,5","20,3","30,2" 

http://en.wikipedia.org/wiki/Comma-separated_values

+2
source

This problem is easy to solve: CSV is not a file format definition (despite RFC 4180).

Excel, as an example, reads and saves csv using a localized list separator and localized data formats set by the user. Thus, β€œComma” in CSV does not mean a comma, but a common list separator (for many EU countries, this is a semicolon, as Tim Schmelter said).

If values ​​are enclosed in quotation marks, they are usually treated as strings. Excel still tries to parse the quoted elements of the CSV string, so this may seem to work in very special cases.

The problem is that both the writer and the reader of the CSV file must use the same culture, otherwise problems will arise with almost everything that is not a string (numbers are one, date / time is another source of problems).

I am using an empirically derived date solution consisting of a string of the format yyyy/MM/dd HH:mm:ss . I noticed that it allows the exchange of working dates between Excel and my programs, interchangeable in English or Italian.

I still haven't found a universal solution for decimal numbers. I suspect that scientific notation can do, but did not have time to test.

+1
source

Source: https://habr.com/ru/post/919318/


All Articles