Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV parser

Status
Not open for further replies.

stardv

Technical User
May 27, 2004
33
US
Does anybody know any good free third-party CSV parser modules? I am surprised that MSDN does not have any classes to parse CSV, it is so common procedure.

Thank a lot
 
why don't you do your own procedure?

open the file, read line and split using ','

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
As DaZZleD said, I think you can write your own procedure. In some languages , for instance Perl, the work could be done in few lines. The Split() function is a candidate.
But there is another way to query data from the CSV files (like you do with tables from a database) using the ODBC Microsoft Text Driver – which is installed automatically if you have MSWord installed.
The following should be done:
-create a DSN file for the ODBC Text Driver to point to the folder where csv files are stored
-create a schema.ini file in which each csv file will have a section that describes the record structure (see below)
- If you do reporting, then use Crystal Reports to point to that ODBC and perform queries like you do in any database. The csv files are seen as tables and you can have primary key, links etc…
- If you are not interested for reporting then you can query data using OdbcDataAdapter or OdbcCommand, and DataSet.

Here is an example of schema.ini:

; this is SCHEMA.INI file- this a comment line

[localpayments.dat]
ColNameHeader=False
Format=TabDelimited
MaxScanRows=25
CharacterSet=ANSI
Col1=PAYMENT_DEST Char Width 4
Col2=BANK_ACCOUNT Char Width 20
Col3=BANK_CHEQUE Char Width 16
Col4=BANK_ID Char Width 11
Col5=BANK_BRANCH Char Width 4
Col6=DATE Char Width 8
Col7=LOCATION Char Width 13
Col8=CASHIER Char Width 8
Col9=RECEIPT Char Width 1
Col10=DRAWER Char Width 3
Col11=PAYMENT_AMOUNT Currency
Col12=PAYMENT_METHOD Char Width 1
Col13=TIME Char Width 6
Col14=PAYMENT_TYPE Char Width 3
Col15=CUST_ACCOUNT Char Width 10
Col16=BILLING_NUMBER Char Width 21
Col25=RECEIPT_NUMBER Char Width 5
//…
[sum.dat]
ColNameHeader=False
Format=Delimited(;)
MaxScanRows=25
CharacterSet=ANSI
Col1=CYCLE Char Width 6
Col2=LOCATION Char Width 13
Col3=DRAWER Char Width 3
Col4=CASHIER Char Width 8
Col5=PAYMENT_METHOD Char Width 1
Col6=PAYMENT_AMOUNT Currency
Col7=PAYMENT_TYPE Char Width 30
Col8=BANK_ID Char Width 11
Col9=BANK_BRANCH Char Width 4
Col10=BANK_ACCOUNT Char Width 20
Col11=BANK_CHEQUE Char Width 16

Example of query ( LPM_DAT is an alias table from the localpayments.dat file:
SELECT
LPM_DAT.`PAYMENT_DEST`, LPM_DAT.`DATE`, LPM_DAT.`LOCATION`, LPM_DAT.`CASHIER`, LPM_DAT.`DRAWER`, LPM_DAT.`PAYMENT_AMOUNT`, LPM_DAT.`PAYMENT_METHOD`, LPM_DAT.`TIME`, LPM_DAT.`CUST_ACCOUNT`, LPM_DAT.`BILLING_NUMBER`, LPM_DAT.`CUSTOMER_NAME`
FROM
` localpayments.dat ` LPM_DAT
WHERE
LPM_DAT.`PAYMENT_METHOD` = '1'
ORDER BY
LPM_DAT.`LOCATION` ASC,
LPM_DAT.`DRAWER` ASC,
LPM_DAT.`CASHIER` ASC

-obislavu-
 
Thank you very much for detailed response. It looks like a good idea when you want to manulaly create chema for every file. I am looking for something that can parse generic unknown format csv files. I think I will give up looking for the library and create my own.

Thanks anyway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top