2

I had a .csv file with many lines and 3 columns (separated by ';') with numbers, which I convert to double array[][] Now I've added to more columns of numbers and get an error:

FormatException -> Input string was not in a correct format

I can't find whats wrong because files are identical (but with 2 more columns) My code:

OpenFileDialog fD = new OpenFileDialog();
fD.Title = "select";
fD.Filter = "csv files|*.csv";
fD.InitialDirectory = @"path here";
if (fD.ShowDialog() == DialogResult.OK)
    MessageBox.Show(fD.FileName.ToString());

double[][] termom = File.ReadLines(fD.FileName)
    .Select(l => l.Split(';')
        .Select(n => double.Parse(n))
        .ToArray())
    .ToArray();

Edit Thanks for help with edit - not very used to commands here. Hope i added file right original improved

4

1 回答 1

3

您的文件res1.csv(假设它应该double只包含值)有一些语法错误,例如

1881081,9;6,315177;352,499964; 01,06,1974 ;350,645

是什么意思01,06,1974?我们是否应该忽略逗号(例如,将它们视为某种千位分隔符)?还是日期1 June 1974)?还有一种可能是,派生,所以我们有三个单独的值161974

如果您想忽略逗号(因此01,06,1974将是1061974.0),只需InvariantCulture在解析时指定:

double[][] termom = File.ReadLines(fD.FileName)
  .Select(l => l.Split(';')
    .Select(n => double.Parse(n, CultureInfo.InvariantCulture))
    .ToArray())
  .ToArray();

如果您想将其,视为派生者(因此01,06,1974将是[1.0, 6.0, 1974.0]

double[][] termom = File.ReadLines(fD.FileName)
  .Select(l => l.Split(';', ',') // ',' is a deriver as well as ';'
    .Select(n => double.Parse(n, CultureInfo.InvariantCulture))
    .ToArray())
  .ToArray();
于 2016-07-04T10:50:05.823 回答