PC Review


Reply
Thread Tools Rate Thread

Checking a CSV file.

 
 
Smith
Guest
Posts: n/a
 
      17th Apr 2009
Hello,

I'm wrting a program that will take a CSV file and perform a number of check
on the fields in the lines.
The field values can be strings and numbers .
Can someone advice on the best approach to deal with this?

Many thanks in advance

S


 
Reply With Quote
 
 
 
 
Smith
Guest
Posts: n/a
 
      17th Apr 2009






>> I'm wrting a program that will take a CSV file and perform a number of
>> check on the fields in the lines.
>> The field values can be strings and numbers .
>> Can someone advice on the best approach to deal with this?

>
> It's a little difficult unless you actually tell the group what checks you
> intend to perform...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


Well,

I'm just thinking of the way to arrange the logic that does the checks the
checks. The actual check could be a range check on the numbers, or looking
up a name in a database. But i thought this would not really matter. Well,
it is obvious that i could write a method to check each field but this does
not sound "Generic" to me. I think there must be more clever approches maybe
something similar to a way compilers parse code lines?

No worries about the actual CSV parsing of the file as i will use linq to
do this in the way shown below:

varquery = from line in
File.ReadAllLines(FileName)where!line.StartsWith("#")letparts=
line.Split(',')select

new{

ISBN = parts[0],

Title= parts[1],

Publisher = parts[2],



Cheers

S


 
Reply With Quote
 
Pavel Minaev
Guest
Posts: n/a
 
      17th Apr 2009
On Apr 17, 2:06*am, "Smith" <Sm...@pricateemail.com> wrote:
> I'm just thinking of the way to arrange the logic that does the checks the
> checks. The actual check could be *a range check on the numbers, or looking
> up a name in a database. But i thought this would not really matter. Well,
> it is obvious that i could write a method to check each field but this does
> not sound "Generic" to me. I think there must be more clever approches maybe
> something similar to a way compilers parse *code lines?


If you need to do a distinct check for every field, then you have to
do a distinct check for every field. If it's something sufficiently
complicated, it may be worth refactoring it into a separate method,
and calling that for all fields it applies to (parametrizing as
needed) - that's about as generic as it gets. For simple range checks,
even that is probably superfluous.

> No worries about the actual *CSV parsing of the file as i will use linqto
> do this in the way shown below:
>
> varquery = from line in
> File.ReadAllLines(FileName)where!line.StartsWith("#")letparts=
> line.Split(',')select
>
> *new{
>
> ISBN = parts[0],
>
> Title= parts[1],
>
> Publisher = parts[2],


You're aware that this reads the entire file into memory at once,
right? It may not be a problem, depending on the size of the file, but
still worth keeping it in mind.

Since you're working with specific fields here anyway (you enumerate
them inside "new {...}"), it would seem that doing the same for
verification is the most reasonable approach.
 
Reply With Quote
 
Smith
Guest
Posts: n/a
 
      17th Apr 2009

"Pavel Minaev" <(E-Mail Removed)> wrote in message
news:99258426-23ef-4371-812d-(E-Mail Removed)...
On Apr 17, 2:06 am, "Smith" <Sm...@pricateemail.com> wrote:
> I'm just thinking of the way to arrange the logic that does the checks the
> checks. The actual check could be a range check on the numbers, or looking
> up a name in a database. But i thought this would not really matter. Well,
> it is obvious that i could write a method to check each field but this
> does
> not sound "Generic" to me. I think there must be more clever approches
> maybe
> something similar to a way compilers parse code lines?


If you need to do a distinct check for every field, then you have to
do a distinct check for every field. If it's something sufficiently
complicated, it may be worth refactoring it into a separate method,
and calling that for all fields it applies to (parametrizing as
needed) - that's about as generic as it gets. For simple range checks,
even that is probably superfluous.

> No worries about the actual CSV parsing of the file as i will use linq to
> do this in the way shown below:
>
> varquery = from line in
> File.ReadAllLines(FileName)where!line.StartsWith("#")letparts=
> line.Split(',')select
>
> new{
>
> ISBN = parts[0],
>
> Title= parts[1],
>
> Publisher = parts[2],


You're aware that this reads the entire file into memory at once,
right? It may not be a problem, depending on the size of the file, but
still worth keeping it in mind.


Since you're working with specific fields here anyway (you enumerate
them inside "new {...}"), it would seem that doing the same for
verification is the most reasonable approach.


1)Yes, you are correct í will fix it with the following:
public static classStreamReaderEnumerable{public static IEnumerable<string>
Lines(this StreamReader source){stringline;if (source == null) throw new
ArgumentNullException("source");while((line = source.ReadLine()) !=
null)yieldreturnline;}



2) please explain furether. I did not get it






 
Reply With Quote
 
Paul Shapiro
Guest
Posts: n/a
 
      17th Apr 2009
Assuming you're also working with a database, you could load the file into a
staging table and use SQL for your testing.

"Smith" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello,
>
> I'm wrting a program that will take a CSV file and perform a number of
> check on the fields in the lines.
> The field values can be strings and numbers .
> Can someone advice on the best approach to deal with this?
>
> Many thanks in advance
> S


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      17th Apr 2009
"Smith" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> I'm wrting a program that will take a CSV file and perform a number of
> check on the fields in the lines.
> The field values can be strings and numbers .
> Can someone advice on the best approach to deal with this?


I used the following project as the basis for my own text parser and I
highly recommend it:
http://www.codeproject.com/KB/databa...ricParser.aspx.


 
Reply With Quote
 
Dude
Guest
Posts: n/a
 
      17th Apr 2009
On Apr 17, 4:16*am, "Smith" <Sm...@pricateemail.com> wrote:
> Hello,
>
> I'm wrting a program that will take a CSV file and perform a number of check
> on the fields in the lines.
> The field values can be strings and *numbers .
> Can someone advice on the best approach to deal with this?
>
> Many thanks in advance
>
> S


Create a class that takes a string as a constructor.

Create a property for each field in the CSV file.

I also usually add a bool IsBad property to the class. Use the IsBad
to validate the data.

List<MyRecord> mListOfRecords = new List<MyRecord>();

....
loop through records
mListOfRecords.add(new MyRecord(theLine));

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking URL for file in VB.net how can it be done? Daniel Padron Microsoft VB .NET 6 1st Feb 2007 10:16 PM
Conflicting used space information when checking the drive property against marking all files and checking the marked file properties. elloko Windows XP Configuration 3 19th Dec 2004 05:34 AM
File checking BrettsNEWS Microsoft Access Forms 7 24th Apr 2004 05:08 AM
checking name of file clayton Microsoft Excel Worksheet Functions 2 5th Mar 2004 04:26 PM
File Checking Donald Lloyd Microsoft Excel Programming 2 3rd Aug 2003 04:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:16 PM.