Reputation: 9
i'm trying to download a csv file from an URL, read it and transform it's data into a visualization via HTML, so basically an ETL-process. However I'm struggling with averaging the data and getting a result.
I tried checking for average with Console.WriteLine but it's NaN, so there is either a problem with averaging the data or with reading it.
Can anyone help me out and point out where the issue may lie?
using System;
using System.Net;
using System.IO;
using System.Linq;
namespace ETL
{
class Program
{
static void Main(string[] args)
{
string url = "https://www.file-upload.net/download-15071709/data.csv.html";
WebClient client = new WebClient();
string data = client.DownloadString(url);
string fileName = "one.csv";
File.WriteAllText(fileName, data);
string[] lines = File.ReadAllLines(fileName);
double sum = 0;
int count = 0;
foreach (string line in lines)
{
string[] values = line.Split(',');
if (values[0] == "Strom")
{
sum += double.Parse(values[1]);
count++;
}
}
double average = sum / count;
string html = $"<html><body><p>Average power consumption is: {average}</p></body></html>";
File.WriteAllText("index.html", html);
}
}
}
Thank you in advance for any help! :D
Upvotes: 0
Views: 70
Reputation: 174
Your code is actually fine but the URL is not a download link for the CSV file. Therefore, you're parsing the web form. In other words: raw HTML code.
I tried your code with https://filesamples.com/samples/document/csv/sample4.csv
and it returns the CSV correctly.
Upvotes: 4