简体   繁体   English

在C#中读取CSV时如何将科学计数法值解析回原始值

[英]How To Parse a Scientific Notation Value Back To Original Value When Reading CSV In C#

I am working on a weather map application that imports csv files via a url. 我正在使用通过URL导入csv文件的天气地图应用程序。 The date fields inside the csv are stored as a string like 201601280330, which would be today's date 1/28/2016 3:30. csv中的日期字段存储为类似于201601280330的字符串,该字符串将是今天的日期1/28/2016 3:30。 However, when reading the csv via a streamreader the value is coming back in scientific notation like 2.01601E+11. 但是,通过流读取器读取csv时,该值会以科学记数法返回,例如2.01601E + 11。 Nothing I have tried seems to return the whole value 201601280330. 我尝试过的所有操作似乎都没有返回整个值201601280330。

For example: 例如:

var date = "2.01601E+11"; var d = Decimal.Parse(date, System.Globalization.NumberStyles.Float);

returns 201601000000 chopping off the remaining part of the string. 返回201601000000截断字符串的剩余部分。 Does anyone know a way to return the full value. 有谁知道返回全部价值的方法。 When I save the csv locally as a text file the correct value 201601280330 is saved rather than 2.01601E+11 . 当我在本地将csv保存为文本文件时,将保存正确的值201601280330而不是2.01601E+11 Anyway to get this in the code without having to save first? 无论如何要在代码中获取此信息而不必先保存? I am using the code below to read the csv file. 我正在使用下面的代码读取csv文件。

    public static DataTable GetDataTableFromCSVFile(string path)
    {
        DataTable dt = new DataTable();
        try
        {
            using (StreamReader sr = new StreamReader(path))
            {
                string[] headers = sr.ReadLine().Split(',');

                foreach (string header in headers)
                {
                    if (header.Contains("ERROR:"))
                    {
                        return null;
                    }

                    dt.Columns.Add(header);
                }
                while (!sr.EndOfStream)
                {
                    string[] rows = sr.ReadLine().Split(',');
                    DataRow dr = dt.NewRow();
                    for (int i = 0; i < headers.Length; i++)
                    {
                        dr[i] = rows[i];
                    }
                    dt.Rows.Add(dr);
                }
            }
        }

EDIT 编辑

Although this does not provide a technical answer to the problem I was experiencing it might provide a better explanation and help others if they experience this. 尽管这不能为我遇到的问题提供技术上的答案,但它可能会提供更好的解释,并在其他人遇到此问题时提供帮助。 Some of the example csv files sent to me for testing had been edited in Excel to make the csv smaller and then saved. 发送给我进行测试的某些示例csv文件已在Excel中进行了编辑,以使csv变小然后保存。 After further testing, it looks like the issue occurs only for the csv files edited in Excel. 经过进一步测试后,似乎仅在Excel中编辑的csv文件会出现此问题。 Tentatively, this solves the problem for me since we won't be downloading the csv files and editing them in Excel, rather pulling them straight from a url. 暂时,这为我解决了问题,因为我们不会下载csv文件并在Excel中进行编辑,而是直接从url中提取它们。 The code I posted above will read a the correct value without notation while for the csv edited in Excel it will only read the value with scientific notation. 我上面发布的代码将读取不带符号的正确值,而对于在Excel中编辑的csv,它将仅读取具有科学符号的值。 Unless I am wrong, I assume Excel must add something to edited csv files that prevents the value from being read correctly. 除非我错了,否则我认为Excel必须向已编辑的csv文件中添加一些内容,以防止正确读取该值。

My original answer questioned whether you were using Excel, but as you hadn't mentioned that in your question I was rightly told that I was off topic, so I changed it. 我最初的回答是询问您是否正在使用Excel,但是由于您没有提到在您的问题中我被正确告知我已脱离话题,所以我对其进行了更改。 Now that you have provided a follow-up answer that does mention Excel I have changed it back, here is what I wrote originally: 既然您提供了一个提到Excel的后续答案,我将其改回了,这就是我最初写的内容:

Once the value has been converted into scientific notation it cannot be converted back. 一旦将值转换为科学计数法,就无法将其转换回。 It is a limitation of Excel that is performing this conversion. 这是执行此转换的Excel的局限性。 If, when importing the data you choose a column type of Text (rather than General), then the data will be imported verbatim and Excel won't convert it into scientific notation. 如果在导入数据时选择的是“文本”(而不是“常规”)列类型,则将逐字导入数据,而Excel不会将其转换为科学计数法。

As I suspected, it is Excel (not your code) that is changing the numerical data into scientific notation. 正如我所怀疑的,是Excel(不是您的代码)将数值数据转换为科学计数法。 I have seen this problem many times and suggest people DO NOT open CSV files using Excel. 我已经多次看到此问题,建议人们不要使用Excel打开CSV文件。 If you have to, then import rather than open so you can specify the data types of your numerical columns. 如果需要,请导入而不是打开,以便您可以指定数字列的数据类型。

Although this does not provide a technical answer to the problem I was experiencing it might provide a better explanation and help others if they experience this. 尽管这不能为我遇到的问题提供技术上的答案,但它可能会提供更好的解释,并在其他人遇到此问题时提供帮助。 Some of the example csv files sent to me for testing had been edited in Excel to make the csv smaller and then saved. 发送给我进行测试的某些示例csv文件已在Excel中进行了编辑,以使csv变小然后保存。 After further testing, it looks like the issue occurs only for the csv files edited in Excel. 经过进一步测试后,似乎仅在Excel中编辑的csv文件会出现此问题。 Tentatively, this solves the problem for me since we won't be downloading the csv files and editing them in Excel, rather pulling them straight from a url. 暂时,这为我解决了问题,因为我们不会下载csv文件并在Excel中进行编辑,而是直接从url中提取它们。 The code I posted above will read a the correct value without notation while for the csv edited in Excel it will only read the value with scientific notation. 我上面发布的代码将读取不带符号的正确值,而对于在Excel中编辑的csv,它将仅读取具有科学符号的值。 Unless I am wrong, I assume Excel must add something to edited csv files that prevents the value from being read correctly. 除非我错了,否则我认为Excel必须向已编辑的csv文件中添加一些内容,以防止正确读取该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM