0

This is the first time I'm working with an API and I have used httpClient.getAsync(uri) to get a response in form of a CSV. What CSV looks like when made into a string.

I'm wondering how to trim or delete parts of it to get a hold of some values. I've tried splitting it using ';' but There is so much clutter and none of what I have tried so far has worked. The 4 values I'm trying to get here are 65.7, 45.3, 64.4, 111.1 and all are located between 2 of these ';' in the middle of the file. In the form of a CSV file, it would be the column 3 (0-3) that I want.

 httpClient.BaseAddress = new Uri("https://opendata-download-metobs.smhi.se/api.json");               
 httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));               
 using (HttpResponseMessage response = await httpClient.GetAsync(new Uri("https://opendata-download-metobs.smhi.se/api/version/latest/parameter/23/station/53430/period/latest-months/data.csv")))     

                {                        
                    res = MakeReadable(response);
                    Console.WriteLine(res);
                    noteMessage(ConsoleColor.Red, "Csv Processing");

                    string s = res.Substring(res.LastIndexOf("Från"));

                    Console.WriteLine(res);

                    Console.WriteLine(
                        response.IsSuccessStatusCode
                            ? "Verification successful"
                            : $"Verification failure: {response.ReasonPhrase}");

                }
            }

The MakeReadable looks like this:

public static string MakeReadable(HttpResponseMessage apiResponse)
    {
        string temp = "";
        HttpContent responseContent = apiResponse.Content;
        Task<string> results = responseContent.ReadAsStringAsync();
        temp = results.Result;
        return temp;
    }

What I don't understand is what Method to use. I have tried using .Remove() with a start and an end but it does nothing. I've tried cleaning strimg of everything but digits, but then i end up with a lot of numbers which also fails me. In the code above I even try Substring + LastIndexOf just to remove unneccessary parts of the string, but nothing works.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 7
    There are plenty of resources out there to read in a CSV file and target specific columns and fields. Have you looked? Don't re-invent the wheel. – rory.ap Oct 28 '19 at 13:03
  • 1
    Sounds like you are applying the split function to the entire file. First step would be to split the file into separate rows. Then loop through the rows and split by the ; separator. That way you can then easily access specific column values by index. – Gary Oct 28 '19 at 13:06
  • 1
    [Check](https://stackoverflow.com/a/2081425/1797425) out this, will indeed help. – Trevor Oct 28 '19 at 13:07
  • 1
    Possible duplicate of [Parsing CSV files in C#, with header](https://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header) or [Reading CSV files using C#](https://stackoverflow.com/questions/3507498/reading-csv-files-using-c-sharp/34196985) or [Reading CSV files](https://stackoverflow.com/questions/1544721/reading-csv-files-in-c-sharp/1544743) – Trevor Oct 28 '19 at 13:07
  • I have looked at it for the past few hours and most likely one to use that I came across was the built in one for .Net. It was the textFieldParser for Microsoft.VisualBasic, but that didn't work either. @rory.ap – user3697903 Oct 28 '19 at 13:08
  • 3
    Unrelated: `Task results = responseContent.ReadAsStringAsync(); temp = results.Result;` - No No. Async all the way. – Fildor Oct 28 '19 at 13:08
  • 1
    I agree with @Fildor, unless this is throw-away code, you should never use Task.Result. You could introduce a deadlock into your program. – rory.ap Oct 28 '19 at 13:34

1 Answers1

-1

It looks like you're after the 3rd column.

You could try:

// Step 1. Split file into lines
var lines = s.Split(new [] { '\r', '\n' });

var indexOfHeaderLine = lines.TakeWhile(t => t.String.StartsWith("Från")).Count();

lines
   .Skip(inextOfHeaderLine+1) //Start after header
   .Select(l => l.Split(';')[2])
   .Where(s => !string.IsNullOrEmpty(s));
tymtam
  • 31,798
  • 8
  • 86
  • 126