Compare numbers and remove numbers from a text file using C# - c#

I have created a text file with some random float numbers ranging from 743.6 to 1500.4.
I am figuring out a way to read the text file (which i have did) and include a number range: lets say( 743.6 <= x <= 800) and remove the numbers which are outside the range and eventually store the final values in a text file.
I have managed to write some codes to read the text file so that when i compile it shows the numbers in the text file. Now i do not know how to progress further . Here is my code, which is able to run compile. This code now ables to read the textfile.
743.6
742.8
744.7
743.2
1000
1768.6
1750
1767
1780
1500
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ReadTextFile
{
class Program
{
static void Main(string[] args)
{
string filePath = "C:\\Users\\Student\\Desktop\\ConsoleApp1\\ConsoleApp1\\Data\\TextFile.txt"; // File Direcotry
List<string> lines = File.ReadAllLines(filePath).ToList();
foreach (string line in lines)
{
Console.WriteLine(line);
}
Console.ReadLine();
}
}
}

This will read the file into memory, parse it, filter it and than overwrite the existing file with the new data.
File.WriteAllLines(filePath,
File.ReadAllLines(filePath)
.Select(x => double.Parse(x))
.Where(x => x >= 800.5 && x <= 850.5)
.Select(x => x.ToString()));

Here's my solution w/ basic error detection and some robustness thanks to the use of regular expressions.
As a foreword: Using regular expressions can be quite expensive and they are not always the way to go.
In this case I think they're okay, because you're handling user-generated input (possibly).
Regular expressions can be optimised by pre-compiling them!
/*
using System;
using System.IO;
using System.Text.RegularExpressions;
*/
void ReadFile(string filePath) {
var fileInfo = default(FileInfo);
var separator = #"[;\s:,]"; // This is a simple RegEx, can be done otherwise. This allows for a little more robustness IMO
// VERY rudimentary error detection
if (string.IsNullOrEmpty(filePath))
throw new ArgumentNullException(nameof(filePath), "The path to the file must not be null or empty!");
try {
fileInfo = new FileInfo(filePath);
} catch {
throw new ArgumentException(nameof(filePath), "A valid path must be given!");
}
if (!fileInfo.Exists) {
throw new IOException(string.Format("The file {0} does not exist!", filePath));
}
// END VERY rudimentary error checking
var numberStrings = Regex.Split(File.ReadAllText(fileInfo.FullName), separator);
// numberStrings is now an array of strings
foreach (var numString in numberStrings) {
if (decimal.TryParse(numString, out var myDecimal)) {
// Do something w/ number
} else {
Debug.WriteLine("{0} is NaN!", numString);
}
}
}
Here's what the code does (written off the top of my head, please don't just C&P it. Test it first):
At first we're defining the regular expression. This matches any character in the range (between the brackets).
Then we're performing very basic error checking:
If the argument passed is null or empty throw an exception
If we couldn't parse the argument to a FileInfo object, the path is likely invalid. Throw an exception.
If the file doesn't exist, throw an exception.
Next we're reading the entire text file in to memory (not on a per-line basis!) and using the regular expression we've defined to split the entire string in to an array of strings.
At last we're looping through our array of strings and parsing each number to a float (that's what you wanted. I personally would use a double or decimal for more precision. See this video from Tom Scott.).
If the string doesn't parse to a float, then you can handle the error accordingly. Otherwise do what you need to with the variable myFloat.
EDIT:
I thought I read you wanting to use floats. My mistake; I changed the datatype to decimal.

You need to read all the lines and replace all values between your min and max value with an empty string:
float min = 800.5F, max = 850.5F;
float currentValue;
var lines = File.ReadAllLines(usersPath);
var separator = ';'; // Change this according to which separator you're using between your values (if any)
foreach (var line in lines)
{
foreach (string word in line.Trim().Split(separator))
{
if (float.TryParse(word.Trim(), out currentValue))
{
if (currentValue < min || currentValue > max)
{
line.Replace(word, "");
}
}
}
}
File.WriteAllLines(usersPath, lines);

Related

How to count semicolon for each line of an csv file in c#

I make an console app, this app is reading csv files using linq to load every line of the file into an IEnumerable.
var lines = from rawLine in File.ReadLines(readFolderFile, Encoding.Default) where !string.IsNullOrEmpty(rawLine) && !string.IsNullOrEmpty(rawLine.Trim(';')) select rawLine;
Now I need to check how many semicolon every line compared to the first line has and if a line has more semicolon than the first one it will be added to an errorList.
So my question is there any easy way to just count the amount of an specific symbol per line?
The outcome should be that I can say, after my source file is proceed with this app, that every row has the identical amount of columns.
Remember every string could be treated as IEnumerable<char> so:
using System.Linq;
...
"my;string;from;csv;file".Count(c => c.Equals(';')); // = 4;
...
You can use this:
int count = source.Count(f => f == ';');
where source is string variable.
So in your case it will look like:
foreach (var line in lines)
{
if (line.Count(f => f == ';') != firstLineCount))
{
//your logic here
}
}

Cannot convert data from textfile to array using .Split (C# Unity3D)

I have a (perhaps) simple question about reading a text file in Unity (C#) containing a matrix of floats, and converting it to a matrix (or multiple arrays) of floats. Before even getting there, I seem unable to succesfully convert the 'string'-typed text to an array using the .Split method.
To summarize, I need the weights of an artificial neural network, which are trained in MATLAB, in my Unity script to use for calculating outputs and performing certain actions.
I will show you carefully what I tried; First of all, the "WeightsIH.txt" file I intend to read looks like this:
-0.493654767886117 1.96685485682002
-0.493654767886117 1.96685485682002
-1.12167737625244 -0.835107290621868
-0.168882176970878 -0.0508678270221517
0.00848314746590752 0.645890421329432
-0.445148017062813 -0.647593931130814
0.0719861441229913 0.00251854104000363
-0.0809087627607675 -0.00253116474618752
0.0677112130287254 0.00229085080614581
0.0754386783608645 0.00239167974789985
0.0809669296303145 0.00253343860819182
-3.54887781611766 -0.884835613897628
-0.459886351851150 -0.848445431434901
-0.0670274486060166 -1.39397672397051
-3.82077442331056 -2.40290337409384
0.0783082838340459 0.00245132734091342
-0.239255503684809 -0.0118048053697862
-0.0798562101865881 -0.00249224979249840
-0.0639184016149756 -0.00224822783089479
-0.778070988555323 -0.872732391008980
0.0297507291420014 -1.74952458318966
0.0963966379119354 0.00416637970011285
0.875794416782941 0.513267873404847
0.0788049456605797 0.00246400296768071
-0.301882135742981 1.29009004735214
-0.427112778202037 -0.602081710823938
-0.0287160010856207 0.876736618413824
0.174484840994587 -0.914135602108887
-1.13771093704277 -1.80803211709460
-0.842897124110693 -0.491320433352398
-0.883862027266628 0.577867664170583
-0.00732465337711950 -0.0608133682721268
0.0808154577205481 0.00252756604265255
-0.623653482256716 0.802021714172196
0.354715179032082 -1.40951283673210
0.107130434979695 0.00718795362864938
-3.25429700675793 1.15179026184665
0.00323132998826740 0.725967105940923
-0.445271160376852 -0.634848835782688
0.0353267566953751 -0.761005808087746
0.343818927585420 0.181552084533698
1.52333372694938 -1.95500299702055
1.28692979700544 2.03963829780562
0.665570336193150 -0.410729910590931
-0.0861536175683719 -0.00286332759826070
0.126619076047933 0.0171533754647685
-0.0822566525595005 -0.00259193055392493
-1.28712374245194 1.12380947288866
-1.29253445353219 -2.05175940341935
0.416493102590467 -0.617909454448863
0.0969179981825367 0.182721981022912
-0.0808788394332979 -0.00252999992128388
0.925818615324062 -1.91710736361040
-0.438361270919922 0.0119396635243124
1.05250770533487 -0.965588245752274
-0.0480257526132498 -0.00154845733604858
-0.0586872685404968 -0.00184430469780941
-0.471992797137374 -0.672492593551730
0.439729645530884 1.55878656878788
1.68156076364744 1.32277838623733
-0.455916176089339 -0.632974493608950
-2.76038741468600 1.87628535124240
0.993963121269136 0.412382925821147
0.0813294944564905 0.00254834378164385
1.05785147371486 -0.713165079912121
0.542621317508334 0.263699015691544
0.0859471661820603 0.00284559667679037
0.0752254002940845 0.00264837949098051
-0.0821494531270125 -0.00258646752960803
-0.135286303232541 -0.0230503985166856
-1.04824146276167 0.379479302836832
-1.00411135626130 0.643815076295448
-1.06427192746705 -1.71485567599474
0.0306923117644997 -0.326399702175058
-0.269230352435044 1.15492815472443
-1.09071040094221 0.974587786723127
-0.0811137162377503 -0.00253932111442298
0.843300471645878 -0.443547135621555
2.62543922875650 -1.43287453338882
-0.0879222032042109 -0.00305697486042674
1.08943207983567 -0.751402936369758
-0.0807147111768526 -0.00252376120454932
0.0920673615786699 0.00345537914425264
-3.32572250550751 2.23334579177979
0.567347532732561 -0.849919751538726
-0.981291377531284 -1.65375737600341
0.717607651399507 -0.501535458162733
Now my C# Unity code looks like this:
using UnityEngine;
using System.Collections;
using System;
using System.IO;
public class WeightsMDF : MonoBehaviour
{
private string text;
void Start ()
{
FileInfo theSourceFile = new FileInfo
("C:/Users/Ajdin/Downloads/UnitySpace/minorProject/Nerd/Nerds/Assets/Scenes/WeightsIH.txt");
StreamReader reader = theSourceFile.OpenText();
do
{
text = reader.ReadLine();
string[] floats = text.Split(" "[0]);
//Console.WriteLine(text);
print (text);
//print(floats[0]);
} while (text != null);
}
}
Now this code gives me the entire textfile in the console, but with a nullreferenceexeption at last:
NullReferenceException: Object reference not set to an instance of an object
WeightsMDF.Start () (at Assets/Scripts/Nerds/WeightsMDF.cs:19).
This exception refers to line 19:
string[] floats = text.Split(" "[0]);
Now if I comment this line, I get the same output except the last console output line says
Null
And If I change line 19 to:
string[] floats = text.Split(" ");//[0]);
I get the following compiling error:
Assets/Scripts/Nerds/WeightsMDF.cs(19,36): error CS1502: The best overloaded method match for `string.Split(params char[])' has some invalid arguments
Assets/Scripts/Nerds/WeightsMDF.cs(19,36): error CS1503: Argument `#1' cannot convert `string' expression to type `char[]'
Does anyone know what I'm doing wrongly?
Thanks in advance!
Try to check text before use split. ReadLine returns null at the end of file, so you need check it before split.
text = reader.ReadLine();
if (text == null)
break;
string[] floats = text.Split(" "[0]);
Also, you can use Peek. It returns next symbol and doesn't use it and -1 if no symbols left.
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
text = sr.ReadLine();
var splits = text.Split (new string[] {" "}, StringSplitOptions.RemoveEmptyEntries);
}
}
To split string by whitespace you can use
string[] splits = text.Split(null);
string[] splits = text.Split(new char[0]);
Instad of doing a do while loop, use a while loop with the condition: (!reader.EndOfStream).
For the split problem you need to send as parameter a char and not a string. with ' ' instead of " " like:
string[] floats = text.split(' ');

How to split at every second quotation mark

I have a string that looks like this
2,"E2002084700801601390870F"
3,"E2002084700801601390870F"
1,"E2002084700801601390870F"
4,"E2002084700801601390870F"
3,"E2002084700801601390870F"
This is one whole string, you can imagine it being on one row.
And I want to split this in the way they stand right now like this
2,"E2002084700801601390870F"
I cannot change the way it is formatted. So my best bet is to split at every second quotation mark. But I haven't found any good ways to do this. I've tried this https://stackoverflow.com/a/17892392/2914876 But I only get an error about invalid arguements.
Another issue is that this project is running .NET 2.0 so most LINQ functions aren't available.
Thank you.
Try this
var regEx = new Regex(#"\d+\,"".*?""");
var lines = regex.Matches(txt).OfType<Match>().Select(m => m.Value).ToArray();
Use foreach instead of LINQ Select on .Net 2
Regex regEx = new Regex(#"\d+\,"".*?""");
foreach(Match m in regex.Matches(txt))
{
var curLine = m.Value;
}
I see three possibilities, none of them are particularly exciting.
As #dvnrrs suggests, if there's no comma where you have line-breaks, you should be in great shape. Replace ," with something novel. Replace the remaining "s with what you need. Replace the "something novel" with ," to restore them. This is probably the most solid--it solves the problem without much room for bugs.
Iterate through the string looking for the index of the next " from the previous index, and maintain a state machine to decide whether to manipulate it or not.
Split the string on "s and rejoin them in whatever way works the best for your application.
I realize regular expressions will handle this but here's a pure 2.0 way to handle as well. It's much more readable and maintainable in my humble opinion.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
const string data = #"2,""E2002084700801601390870F""3,""E2002084700801601390870F""1,""E2002084700801601390870F""4,""E2002084700801601390870F""3,""E2002084700801601390870F""";
var parsedData = ParseData(data);
foreach (var parsedDatum in parsedData)
{
Console.WriteLine(parsedDatum);
}
Console.ReadLine();
}
private static IEnumerable<string> ParseData(string data)
{
var results = new List<string>();
var split = data.Split(new [] {'"'}, StringSplitOptions.RemoveEmptyEntries);
if (split.Length % 2 != 0)
{
throw new Exception("Data Formatting Error");
}
for (var index = 0; index < split.Length / 2; index += 2)
{
results.Add(string.Format(#"""{0}""{1}""", split[index], split[index + 1]));
}
return results;
}
}
}

Search and replace values in text file with C#

I have a text file with a certain format. First comes an identifier followed by three spaces and a colon. Then comes the value for this identifier.
ID1 :Value1
ID2 :Value2
ID3 :Value3
What I need to do is searching e.g. for ID2 : and replace Value2 with a new value NewValue2. What would be a way to do this? The files I need to parse won't get very large. The largest will be around 150 lines.
If the file isn't that big you can do a File.ReadAllLines to get a collection of all the lines and then replace the line you're looking for like this
using System.IO;
using System.Linq;
using System.Collections.Generic;
List<string> lines = new List<string>(File.ReadAllLines("file"));
int lineIndex = lines.FindIndex(line => line.StartsWith("ID2 :"));
if (lineIndex != -1)
{
lines[lineIndex] = "ID2 :NewValue2";
File.WriteAllLines("file", lines);
}
Here's a simple solution which also creates a backup of the source file automatically.
The replacements are stored in a Dictionary object. They are keyed on the line's ID, e.g. 'ID2' and the value is the string replacement required. Just use Add() to add more as required.
StreamWriter writer = null;
Dictionary<string, string> replacements = new Dictionary<string, string>();
replacements.Add("ID2", "NewValue2");
// ... further replacement entries ...
using (writer = File.CreateText("output.txt"))
{
foreach (string line in File.ReadLines("input.txt"))
{
bool replacementMade = false;
foreach (var replacement in replacements)
{
if (line.StartsWith(replacement.Key))
{
writer.WriteLine(string.Format("{0} :{1}",
replacement.Key, replacement.Value));
replacementMade = true;
break;
}
}
if (!replacementMade)
{
writer.WriteLine(line);
}
}
}
File.Replace("output.txt", "input.txt", "input.bak");
You'll just have to replace input.txt, output.txt and input.bak with the paths to your source, destination and backup files.
Ordinarily, for any text searching and replacement, I'd suggest some sort of regular expression work, but if this is all you're doing, that's really overkill.
I would just open the original file and a temporary file; read the original a line at a time, and just check each line for "ID2 :"; if you find it, write your replacement string to the temporary file, otherwise, just write what you read. When you've run out of source, close both, delete the original, and rename the temporary file to that of the original.
Something like this should work. It's very simple, not the most efficient thing, but for small files, it would be just fine:
private void setValue(string filePath, string key, string value)
{
string[] lines= File.ReadAllLines(filePath);
for(int x = 0; x < lines.Length; x++)
{
string[] fields = lines[x].Split(':');
if (fields[0].TrimEnd() == key)
{
lines[x] = fields[0] + ':' + value;
File.WriteAllLines(lines);
break;
}
}
}
You can use regex and do it in 3 lines of code
string text = File.ReadAllText("sourcefile.txt");
text = Regex.Replace(text, #"(?i)(?<=^id2\s*?:\s*?)\w*?(?=\s*?$)", "NewValue2",
RegexOptions.Multiline);
File.WriteAllText("outputfile.txt", text);
In the regex, (?i)(?<=^id2\s*?:\s*?)\w*?(?=\s*?$) means, find anything that starts with id2 with any number of spaces before and after :, and replace the following string (any alpha numeric character, excluding punctuations) all the way 'till end of the line. If you want to include punctuations, then replace \w*? with .*?
You can use regexes to achieve this.
Regex re = new Regex(#"^ID\d+ :Value(\d+)\s*$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
List<string> lines = File.ReadAllLines("mytextfile");
foreach (string line in lines) {
string replaced = re.Replace(target, processMatch);
//Now do what you going to do with the value
}
string processMatch(Match m)
{
var number = m.Groups[1];
return String.Format("ID{0} :NewValue{0}", number);
}

How do i parse a text file in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
How do i parse a text file in c#?
Check this interesting approach, Linq To Text Files, very nice, you only need a IEnumerable<string> method, that yields every file.ReadLine(), and you do the query.
Here is another article that better explains the same technique.
using (TextReader rdr = new StreamReader(fullFilePath))
{
string line;
while ((line = rdr.ReadLine()) != null)
{
// use line here
}
}
set the variable "fullFilePath" to the full path eg. C:\temp\myTextFile.txt
The algorithm might look like this:
Open Text File
For every line in the file:
Parse Line
There are several approaches to parsing a line.
The easiest from a beginner standpoint is to use the String methods.
System.String at MSDN
If you are up for more of a challenge, then you can use the System.Text.RegularExpression library to parse your text.
RegEx at MSDN
You might want to use a helper class such as the one described at http://www.blackbeltcoder.com/Articles/strings/a-text-parsing-helper-class.
From years of analyzing CSV files, including ones that are broken or have edge cases, here is my code that passes virtually all of my unit tests:
/// <summary>
/// Read in a line of text, and use the Add() function to add these items to the current CSV structure
/// </summary>
/// <param name="s"></param>
public static bool TryParseCSVLine(string s, char delimiter, char text_qualifier, out string[] array)
{
bool success = true;
List<string> list = new List<string>();
StringBuilder work = new StringBuilder();
for (int i = 0; i < s.Length; i++) {
char c = s[i];
// If we are starting a new field, is this field text qualified?
if ((c == text_qualifier) && (work.Length == 0)) {
int p2;
while (true) {
p2 = s.IndexOf(text_qualifier, i + 1);
// for some reason, this text qualifier is broken
if (p2 < 0) {
work.Append(s.Substring(i + 1));
i = s.Length;
success = false;
break;
}
// Append this qualified string
work.Append(s.Substring(i + 1, p2 - i - 1));
i = p2;
// If this is a double quote, keep going!
if (((p2 + 1) < s.Length) && (s[p2 + 1] == text_qualifier)) {
work.Append(text_qualifier);
i++;
// otherwise, this is a single qualifier, we're done
} else {
break;
}
}
// Does this start a new field?
} else if (c == delimiter) {
list.Add(work.ToString());
work.Length = 0;
// Test for special case: when the user has written a casual comma, space, and text qualifier, skip the space
// Checks if the second parameter of the if statement will pass through successfully
// e.g. "bob", "mary", "bill"
if (i + 2 <= s.Length - 1) {
if (s[i + 1].Equals(' ') && s[i + 2].Equals(text_qualifier)) {
i++;
}
}
} else {
work.Append(c);
}
}
list.Add(work.ToString());
// If we have nothing in the list, and it's possible that this might be a tab delimited list, try that before giving up
if (list.Count == 1 && delimiter != DEFAULT_TAB_DELIMITER) {
string[] tab_delimited_array = ParseLine(s, DEFAULT_TAB_DELIMITER, DEFAULT_QUALIFIER);
if (tab_delimited_array.Length > list.Count) {
array = tab_delimited_array;
return success;
}
}
// Return the array we parsed
array = list.ToArray();
return success;
}
However, this function does not actually parse every valid CSV file out there! Some files have embedded newlines in them, and you need to enable your stream reader to parse multiple lines together to return an array. Here's a tool that does that:
/// <summary>
/// Parse a line whose values may include newline symbols or CR/LF
/// </summary>
/// <param name="sr"></param>
/// <returns></returns>
public static string[] ParseMultiLine(StreamReader sr, char delimiter, char text_qualifier)
{
StringBuilder sb = new StringBuilder();
string[] array = null;
while (!sr.EndOfStream) {
// Read in a line
sb.Append(sr.ReadLine());
// Does it parse?
string s = sb.ToString();
if (TryParseCSVLine(s, delimiter, text_qualifier, out array)) {
return array;
}
}
// Fails to parse - return the best array we were able to get
return array;
}
For reference, I placed my open source CSV code on code.google.com.
If you have more than a trivial language, use a parser generator. It drove me nuts but I've heard good things about ANTLR (Note: get the manual and read it before you start. If you have used a parser generator other than it before you will not approach it correctly right off the bat, at least I didn't)
Other tools also exist.
What do you mean by parse? Parse usually means to split the input into tokens, which you might do if you're trying to implement a programming language. If you're just wanting to read the contents of a text file, look at System.IO.FileInfo.
Without really knowing what sort of text file you're on about, its hard to answer. However, the FileHelpers library has a broad set of tools to help with fixed length file formats, multirecord, delimited etc.
A small improvement on Pero's answer:
FileInfo txtFile = new FileInfo("c:\myfile.txt");
if(!txtFile.Exists) { // error handling }
using (TextReader rdr = txtFile.OpenText())
{
// use the text file as Pero suggested
}
The FileInfo class gives you the opportunity to "do stuff" with the file before you actually start reading from it. You can also pass it around between functions as a better abstraction of the file's location (rather than using the full path string). FileInfo canonicalizes the path so it's absolutely correct (e.g. turning / into \ where appropriate) and lets you extract extra data about the file -- parent directory, extension, name only, permissions, etc.
To begin with, make sure that you have the following namespaces:
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
Next, we build a function that parses any CSV input string into a DataTable:
public DataTable ParseCSV(string inputString) {
DataTable dt=new DataTable();
// declare the Regular Expression that will match versus the input string
Regex re=new Regex("((?<field>[^\",\\r\\n]+)|\"(?<field>([^\"]|\"\")+)\")(,|(?<rowbreak>\\r\\n|\\n|$))");
ArrayList colArray=new ArrayList();
ArrayList rowArray=new ArrayList();
int colCount=0;
int maxColCount=0;
string rowbreak="";
string field="";
MatchCollection mc=re.Matches(inputString);
foreach(Match m in mc) {
// retrieve the field and replace two double-quotes with a single double-quote
field=m.Result("${field}").Replace("\"\"","\"");
rowbreak=m.Result("${rowbreak}");
if (field.Length > 0) {
colArray.Add(field);
colCount++;
}
if (rowbreak.Length > 0) {
// add the column array to the row Array List
rowArray.Add(colArray.ToArray());
// create a new Array List to hold the field values
colArray=new ArrayList();
if (colCount > maxColCount)
maxColCount=colCount;
colCount=0;
}
}
if (rowbreak.Length == 0) {
// this is executed when the last line doesn't
// end with a line break
rowArray.Add(colArray.ToArray());
if (colCount > maxColCount)
maxColCount=colCount;
}
// create the columns for the table
for(int i=0; i < maxColCount; i++)
dt.Columns.Add(String.Format("col{0:000}",i));
// convert the row Array List into an Array object for easier access
Array ra=rowArray.ToArray();
for(int i=0; i < ra.Length; i++) {
// create a new DataRow
DataRow dr=dt.NewRow();
// convert the column Array List into an Array object for easier access
Array ca=(Array)(ra.GetValue(i));
// add each field into the new DataRow
for(int j=0; j < ca.Length; j++)
dr[j]=ca.GetValue(j);
// add the new DataRow to the DataTable
dt.Rows.Add(dr);
}
// in case no data was parsed, create a single column
if (dt.Columns.Count == 0)
dt.Columns.Add("NoData");
return dt;
}
Now that we have a parser for converting a string into a DataTable, all we need now is a function that will read the content from a CSV file and pass it to our ParseCSV function:
public DataTable ParseCSVFile(string path) {
string inputString="";
// check that the file exists before opening it
if (File.Exists(path)) {
StreamReader sr = new StreamReader(path);
inputString = sr.ReadToEnd();
sr.Close();
}
return ParseCSV(inputString);
}
And now you can easily fill a DataGrid with data coming off the CSV file:
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e) {
// call the parser
DataTable dt=ParseCSVFile(Server.MapPath("./demo.csv"));
// bind the resulting DataTable to a DataGrid Web Control
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
Congratulations! You are now able to parse CSV into a DataTable. Good luck with your programming.

Categories