7

First of all i'm very new with coding and at the beginner level in c#. I look at the questions but couldn't find proper answer for my question or didn't understand.

I have a .csv file and the lines in the files are like below. I get the longitude value by using float longitude = float.Parse(read[1]); But I also want to get the number "3" in the example of below. In my opinion i can get the value by using float.Parse(read[12]). This number is in the 12th index in the line. But i cannot reach that number by using float.Parse(read[12]). When i use float.Parse(read[9]) i can get the 3. This code is not reading the missing values between commas. How can i read all the data incluiding missing values? Because the other lines are different and missing values are changing. Some of the line doesn't include date information, some of them doesn't include longitude information.

0000000,26.0000000,38.000000,30.01.2017,0,0,0,,,0,0,,3,,0,0,0,0

string[] read;
char[] seperators = { ',' };

StreamReader sr = new StreamReader("D:/xxx.csv");

string data = sr.ReadLine();

while ((data = sr.ReadLine()) != null)
{
    read = data.Split(seperators,StringSplitOptions.RemoveEmptyEntries);
    float longitude = float.Parse(read[1]);
    float latitude = float.Parse(read[2]);
}
5
  • 3
    "This code is not reading the missing values between commas" - please read How to Ask and minimal reproducible example and show the relevant code. Commented Jan 30, 2017 at 11:55
  • 1
    add in how do you get your read[] Commented Jan 30, 2017 at 11:55
  • StreamReader sr = new StreamReader("Document path"); string data = sr.ReadLine(); while ((data = sr.ReadLine()) != null) { read = data.Split(seperators,StringSplitOptions.RemoveEmptyEntries); float longitude = float.Parse(read[1]); float latitude = float.Parse(read[2]); } Commented Jan 30, 2017 at 12:15
  • sorry for the complicate answer. i cannot write the code properly for reading. Commented Jan 30, 2017 at 12:16
  • read = data.Split(seperators,StringSplitOptions.RemoveEmptyEntries); You explicitly skip empty entries, which makes it difficult to count indices. Just leave out that optional parameter, then read = data.Split(seperators,StringSplitOptions.RemoveEmptyEntries); will include the empty parts of your csv-input as well. But make sure, you do not run into exceptions while trying to parse empty strings. Commented Jan 30, 2017 at 12:32

3 Answers 3

9

This line

read = data.Split(seperators, StringSplitOptions.RemoveEmptyEntries);

Says:

If there are two commas, one right after the other, ignore the entry

Hence, your input becomes:

0000000,26.0000000,38.000000,30.01.2017,0,0,0,0,0,3,0,0,0,0

What you really want is this, which leaves empty values in the array

read = data.Split(seperators, StringSplitOptions.None);
Sign up to request clarification or add additional context in comments.

Comments

0

The error is this line:

 read = data.Split(seperators,StringSplitOptions.RemoveEmptyEntries);

You are telling it to remove the empty values. The missing ones you want. Change it to

 read = data.Split(seperators,StringSplitOptions.None);

Comments

0

As others mentioned you must change StringSplitOptions.RemoveEmptyEntries to StringSplitOptions.None.

However, since this is the default parameter you can simply use:

read = data.Split(seperators);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.