473,938 Members | 21,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CSV files

Cat
I don't understand why there's no class included in the libraries for
reading CSV files.. I've created my own CSV reader class which reads a CSV
file, generates a report and returns records etc. Although I'm proud of
having tackled the problem and produced code that works I worry that I could
have saved a lot of time if I could have just found that class in the
library which I'm convinced must work.

Does anyone have an explanation as to why there's no such class?

Cat
Nov 16 '05 #1
13 2359
Cat wrote:
Does anyone have an explanation as to why there's no such class?


perhaps because the format is not /that/ often used and it's fairly
simple so a 30-line-code is sufficent to parse it?

--
Konrad -
http://madrat.net/
Nov 16 '05 #2
Cat,

You can use the classes in the System.Data.Ole Db namespace. Basically,
you will have to use a provider for OLEDB (I believe there is one for comma
delimited text, or any delimited text in general) which will do what you
want. Using that provider, you can query the data like you would any other
data and get the results in a DataReader, or a DataSet.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:eC******** ******@tk2msftn gp13.phx.gbl...
I don't understand why there's no class included in the libraries for
reading CSV files.. I've created my own CSV reader class which reads a CSV
file, generates a report and returns records etc. Although I'm proud of
having tackled the problem and produced code that works I worry that I could have saved a lot of time if I could have just found that class in the
library which I'm convinced must work.

Does anyone have an explanation as to why there's no such class?

Cat

Nov 16 '05 #3
Cat
I wouldn't say the code is fairly simple, since there's a lot of error
checking to be done and decisions to be made depending on where quotation
marks and commas appear in the csv file. Still, I am very much a newbie to
C# and commercial programming in general so I may have a very different
opinion in a year or so.

As for csv files not being used so often - unfortunately due to the nature
of the company I work for I'll be dealing with files like that quite a lot
:(

"Konrad L. M. Rudolph" <ko************ @madrat.net> wrote in message
news:u4******** ******@TK2MSFTN GP09.phx.gbl...
Cat wrote:
Does anyone have an explanation as to why there's no such class?


perhaps because the format is not /that/ often used and it's fairly
simple so a 30-line-code is sufficent to parse it?

--
Konrad -
http://madrat.net/

Nov 16 '05 #4
Cat
Thanks for the advice. I still haven't quite got a handle on how to work
with OleDB, but I'll no doubt work through some of the msdn tutuorials in
the near future.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:Or******** ******@TK2MSFTN GP11.phx.gbl...
Cat,

You can use the classes in the System.Data.Ole Db namespace. Basically, you will have to use a provider for OLEDB (I believe there is one for comma delimited text, or any delimited text in general) which will do what you
want. Using that provider, you can query the data like you would any other data and get the results in a DataReader, or a DataSet.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:eC******** ******@tk2msftn gp13.phx.gbl...
I don't understand why there's no class included in the libraries for
reading CSV files.. I've created my own CSV reader class which reads a CSV file, generates a report and returns records etc. Although I'm proud of
having tackled the problem and produced code that works I worry that I

could
have saved a lot of time if I could have just found that class in the
library which I'm convinced must work.

Does anyone have an explanation as to why there's no such class?

Cat


Nov 16 '05 #5
Hehe LMAO.
Guess what, I did exactly the same thing -was good coding practice anyhow
and importantly was to go through the jobserve.csv file (any guesses why?!)

Then one day I stumbled upon some code and thought "you plum!". This
following code came from the MCAD/ MCSD Developing Windows Based
Applications book from MSPRESS (dont buy it though the dual vb.net/ c#.net
training is a pain in the proverbial). The most important line is the very
last one in the listing.

// This example assumes the existence of a text file named myFile.txt
// that contains an undetermined number of rows with seven entries
// in each row. Creates a new DataSet
DataSet myDataSet = new DataSet();
// Creates a new DataTable and adds it to the Tables collection
DataTable aTable = new DataTable("Tabl e 1");
myDataSet.Table s.Add("Table 1");
// Creates and names seven columns and adds them to Table 1
DataColumn aColumn;
for (int counter = 0; counter < 7; counter ++)
{
aColumn = new DataColumn("Col umn " + counter.ToStrin g());
myDataSet.Table s["Table 1"].Columns.Add(aC olumn);
}
// Creates the StreamReader to read the file and a string variable to
// hold the output of the StreamReader
System.IO.Strea mReader myReader = new
System.IO.Strea mReader("C:\\my File.txt");
string myString;
// Checks to see if the Reader has reached the end of the stream
while (myReader.Peek( ) != –1)
{
// Reads a line of data from the text file
myString = myReader.ReadLi ne();
// Uses the String.Split method to create an array of strings that
// represents each entry in the line. That array is then added as
// a new DataRow to Table 1
myDataSet.Table s["Table 1"].
Rows.Add(myStri ng.Split(char.P arse(",")));
}

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:eC******** ******@tk2msftn gp13.phx.gbl...
I don't understand why there's no class included in the libraries for
reading CSV files.. I've created my own CSV reader class which reads a CSV
file, generates a report and returns records etc. Although I'm proud of
having tackled the problem and produced code that works I worry that I could have saved a lot of time if I could have just found that class in the
library which I'm convinced must work.

Does anyone have an explanation as to why there's no such class?

Cat

Nov 16 '05 #6
Cat
The problem with this code is that it wouldn't know how to deal with a line
like this:

"First Name, Last Name", "Address1, Address2, Address3, Code", Telephone
number

- the code I've been writing has to take into account the fact that commas
can appear in field data and that not all fields will have surrounding
quotations marks. It has to check for errors too, like quotation marks
appearing mid field when the field data did or didn't start with a quotation
mark. Annoyingly makes the file reading code a bit more complex

"Mark Broadbent" <no************ @no-spam-please.com> wrote in message
news:OF******** ******@TK2MSFTN GP11.phx.gbl...
Hehe LMAO.
Guess what, I did exactly the same thing -was good coding practice anyhow
and importantly was to go through the jobserve.csv file (any guesses why?!)
Then one day I stumbled upon some code and thought "you plum!". This
following code came from the MCAD/ MCSD Developing Windows Based
Applications book from MSPRESS (dont buy it though the dual vb.net/ c#.net
training is a pain in the proverbial). The most important line is the very
last one in the listing.

// This example assumes the existence of a text file named myFile.txt
// that contains an undetermined number of rows with seven entries
// in each row. Creates a new DataSet
DataSet myDataSet = new DataSet();
// Creates a new DataTable and adds it to the Tables collection
DataTable aTable = new DataTable("Tabl e 1");
myDataSet.Table s.Add("Table 1");
// Creates and names seven columns and adds them to Table 1
DataColumn aColumn;
for (int counter = 0; counter < 7; counter ++)
{
aColumn = new DataColumn("Col umn " + counter.ToStrin g());
myDataSet.Table s["Table 1"].Columns.Add(aC olumn);
}
// Creates the StreamReader to read the file and a string variable to
// hold the output of the StreamReader
System.IO.Strea mReader myReader = new
System.IO.Strea mReader("C:\\my File.txt");
string myString;
// Checks to see if the Reader has reached the end of the stream
while (myReader.Peek( ) != -1)
{
// Reads a line of data from the text file
myString = myReader.ReadLi ne();
// Uses the String.Split method to create an array of strings that
// represents each entry in the line. That array is then added as
// a new DataRow to Table 1
myDataSet.Table s["Table 1"].
Rows.Add(myStri ng.Split(char.P arse(",")));
}

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:eC******** ******@tk2msftn gp13.phx.gbl...
I don't understand why there's no class included in the libraries for
reading CSV files.. I've created my own CSV reader class which reads a CSV file, generates a report and returns records etc. Although I'm proud of
having tackled the problem and produced code that works I worry that I

could
have saved a lot of time if I could have just found that class in the
library which I'm convinced must work.

Does anyone have an explanation as to why there's no such class?

Cat


Nov 16 '05 #7
I see. :(
You could potentially split on commas (refer to code sample) and for each
string in string array returned split again on " queuing it all up and then
combining it all together. Dont know how much error checking youd need but
obviously it would still be a requirement.
OLEDB classes might be worth a look for this, however its probably easier to
stick with a custom class.

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:u8kfnKQOEH A.4044@TK2MSFTN G'P10.phx.gbl.. .
The problem with this code is that it wouldn't know how to deal with a line like this:

"First Name, Last Name", "Address1, Address2, Address3, Code", Telephone
number

- the code I've been writing has to take into account the fact that commas
can appear in field data and that not all fields will have surrounding
quotations marks. It has to check for errors too, like quotation marks
appearing mid field when the field data did or didn't start with a quotation mark. Annoyingly makes the file reading code a bit more complex

"Mark Broadbent" <no************ @no-spam-please.com> wrote in message
news:OF******** ******@TK2MSFTN GP11.phx.gbl...
Hehe LMAO.
Guess what, I did exactly the same thing -was good coding practice anyhow
and importantly was to go through the jobserve.csv file (any guesses

why?!)

Then one day I stumbled upon some code and thought "you plum!". This
following code came from the MCAD/ MCSD Developing Windows Based
Applications book from MSPRESS (dont buy it though the dual vb.net/ c#.net training is a pain in the proverbial). The most important line is the very last one in the listing.

// This example assumes the existence of a text file named myFile.txt
// that contains an undetermined number of rows with seven entries
// in each row. Creates a new DataSet
DataSet myDataSet = new DataSet();
// Creates a new DataTable and adds it to the Tables collection
DataTable aTable = new DataTable("Tabl e 1");
myDataSet.Table s.Add("Table 1");
// Creates and names seven columns and adds them to Table 1
DataColumn aColumn;
for (int counter = 0; counter < 7; counter ++)
{
aColumn = new DataColumn("Col umn " + counter.ToStrin g());
myDataSet.Table s["Table 1"].Columns.Add(aC olumn);
}
// Creates the StreamReader to read the file and a string variable to
// hold the output of the StreamReader
System.IO.Strea mReader myReader = new
System.IO.Strea mReader("C:\\my File.txt");
string myString;
// Checks to see if the Reader has reached the end of the stream
while (myReader.Peek( ) != -1)
{
// Reads a line of data from the text file
myString = myReader.ReadLi ne();
// Uses the String.Split method to create an array of strings that
// represents each entry in the line. That array is then added as
// a new DataRow to Table 1
myDataSet.Table s["Table 1"].
Rows.Add(myStri ng.Split(char.P arse(",")));
}

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:eC******** ******@tk2msftn gp13.phx.gbl...
I don't understand why there's no class included in the libraries for
reading CSV files.. I've created my own CSV reader class which reads a

CSV file, generates a report and returns records etc. Although I'm proud of having tackled the problem and produced code that works I worry that I

could
have saved a lot of time if I could have just found that class in the
library which I'm convinced must work.

Does anyone have an explanation as to why there's no such class?

Cat



Nov 16 '05 #8

"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:u8******** ******@TK2MSFTN GP10.phx.gbl...
The problem with this code is that it wouldn't know how to deal with a
line
like this:

"First Name, Last Name", "Address1, Address2, Address3, Code", Telephone
number

- the code I've been writing has to take into account the fact that commas
can appear in field data and that not all fields will have surrounding
quotations marks. It has to check for errors too, like quotation marks
appearing mid field when the field data did or didn't start with a
quotation
mark. Annoyingly makes the file reading code a bit more complex
And that very well may hit on the head a good part of why its not
included(not including, of course, that OleDb supports it already). One of
the most annoying things about csv files is the lack of standardization .
Everything from parsing expectations(qu otes required, quotes required only
when containing commas, quotes not allowed) down to any specific escape
sequences your file may use so that quotes or newlines may appear varies
somewhat between files. Writing a generic reader that reads all of the
combonations correctly that may not be possible to do in a way that makes
the generic reader worth the time. Last thing you want to do is provide a
generic reader that no one is happy with.
"Mark Broadbent" <no************ @no-spam-please.com> wrote in message
news:OF******** ******@TK2MSFTN GP11.phx.gbl...
Hehe LMAO.
Guess what, I did exactly the same thing -was good coding practice anyhow
and importantly was to go through the jobserve.csv file (any guesses

why?!)

Then one day I stumbled upon some code and thought "you plum!". This
following code came from the MCAD/ MCSD Developing Windows Based
Applications book from MSPRESS (dont buy it though the dual vb.net/
c#.net
training is a pain in the proverbial). The most important line is the
very
last one in the listing.

// This example assumes the existence of a text file named myFile.txt
// that contains an undetermined number of rows with seven entries
// in each row. Creates a new DataSet
DataSet myDataSet = new DataSet();
// Creates a new DataTable and adds it to the Tables collection
DataTable aTable = new DataTable("Tabl e 1");
myDataSet.Table s.Add("Table 1");
// Creates and names seven columns and adds them to Table 1
DataColumn aColumn;
for (int counter = 0; counter < 7; counter ++)
{
aColumn = new DataColumn("Col umn " + counter.ToStrin g());
myDataSet.Table s["Table 1"].Columns.Add(aC olumn);
}
// Creates the StreamReader to read the file and a string variable to
// hold the output of the StreamReader
System.IO.Strea mReader myReader = new
System.IO.Strea mReader("C:\\my File.txt");
string myString;
// Checks to see if the Reader has reached the end of the stream
while (myReader.Peek( ) != -1)
{
// Reads a line of data from the text file
myString = myReader.ReadLi ne();
// Uses the String.Split method to create an array of strings that
// represents each entry in the line. That array is then added as
// a new DataRow to Table 1
myDataSet.Table s["Table 1"].
Rows.Add(myStri ng.Split(char.P arse(",")));
}

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Cat" <lo***********@ yahoo.co.uk> wrote in message
news:eC******** ******@tk2msftn gp13.phx.gbl...
> I don't understand why there's no class included in the libraries for
> reading CSV files.. I've created my own CSV reader class which reads a CSV > file, generates a report and returns records etc. Although I'm proud of
> having tackled the problem and produced code that works I worry that I

could
> have saved a lot of time if I could have just found that class in the
> library which I'm convinced must work.
>
> Does anyone have an explanation as to why there's no such class?
>
> Cat
>
>



Nov 16 '05 #9
Mark Broadbent wrote:
I see. :(
You could potentially split on commas (refer to code sample) and for each
string in string array returned split again on " queuing it all up and then
combining it all together. Dont know how much error checking youd need but
obviously it would still be a requirement.


I don't want to interrupt ...

but has either of you considered using regex to parse CSV?

--
Konrad -
http://madrat.net/
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14182
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip Smarty-2.6.7.tar.gz on a system running WindowsXP SP2. Apache and PHP tested out fine. After adding Smarty, I ran the following http://localhost/testphp.php
44
4110
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are identical with which. Write a program that prints out which file are redundant copies. Here's the spec. -------------------------- The program is to be used on the command line. Its arguments are one or
0
6166
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug file as folows. I need help to resolve them ASAP: cl /c /nologo /MDd /W3 /Od /GR /GM /Zi /GX /D "_DEBUG" /D " WIN32" /D "_W INDOWS" /D "_WINDLL" /D "_AFXDLL" /D "_MBCS" /D "_USRDLL" /
18
3207
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now you have your ".cpp" files all ready, without any "#include" or "#define" in them. Let's assume that there's 2 source files in this project, "a.cpp" and
3
3113
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by using header file. i can create a class c1 with f1() in c1.h and include this c1.h in file1.cpp by giving the statement #include "c1.h" tell me that what exactly is the difference between c1.h and c1.cpp? Since they both are doing the same things.
11
5639
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how does that get linked to our program when we run it??I would appreciate any helpful info..And I would like to thank you for that in advance -ambika
22
3041
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text files, which in turn could be simply and easily maintained with notepad. I understand the benefits of XML, really, but in the case of configuration files it seems it is almost always nothing more than unnecessary complexity, both in accessing them...
18
2323
by: UJ | last post by:
Folks, We provide custom content for our customers. Currently we put the files on our server and people have a program we provide that will download the files. These files are usually SWF, HTML or JPG files. The problem as I see it - if you know the name of the file, you could download it off the server (currently we are using an HTTP/Get but I'm going to be using WebClient in the new version.) If there any way to password protect the...
0
1529
by: wal | last post by:
How does one attach files to emails using libgmail? The following code http://pramode.net/articles/lfy/fuse/4.txt works fine when said files are simple text files, but it failes as soon as the files are wild binary files, even attaching the source of an email in a text file (.eml files) failes. What am I missing here? Any hints? The output I get when attemting to send binary files using the code above is pasted below. have fun,
3
5210
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
10131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11284
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9854
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.