CSV Files

From CometWiki

(Difference between revisions)
Jump to: navigation, search
Jim (Talk | contribs)
(Initial Construction of page)
Newer edit →

Revision as of 14:58, 21 December 2010

Comma Delimited Files

Syntax:

File(LUN)CSV  ! Treat this text file as Comma delimited

File(LUN)DLM=Str$ ! Use the first character of str$ instead of a comma to delimit fields in this file ("@09@" is a tab)

File(LUN)NORMAL  ! Revert this file to use normal field handling

Discussion:

Comet Text files may be treated as Comma Delimited Files (CSV or Comma Separated Values).

For a discussion of how Comma Delimited Files are formatted, Look Here.


CSV files are a convienient way to treat data for import or export to other windows applications. To tell Comet to start treating a file as CSV,

just execute the fillowing file statement:

File(LUN)CSV

Example 1:

Writing a CSV record.

Length 200  & Local A$ B$ C$  
Length 15.0 & Local I 
Length 15.3  & Local J K 
Length 10.10  & Local L

Fmt:  Format A$;B$;C$;I;J;K;L

Clear
Print(Et)
Erase 'xxx.Csv', Dir='cos' Excp=Eok
Eok: Create 'xxx.Csv', Dir='cos'

Close(1) & Open(1) 'xxx.Csv', Dir='cos'
File(1)Csv
A$ = 'simple String'
B$ = ' String With Embeded Double "Quotation""Marks'        ! Leading Blank
C$ = ' String Including A, Comma '                          ! Trailing Blank
I = -10
J = 10.34
K = -99.345
L = -.00012345
Write(1,Fmt)
Stop

In the above example, three string variables and four numeric variables are written to a record in a text file. The result looks like this:

simple string," String with embeded double ""Quotation""""marks"," String including a, comma",-10,10.340,-99.345,-0.0001234500

Notice that double quotes surround fields containing double quotation marks or commas. Double quotes within a field are escaped by placing another double quote before them. Leading minus signs are used for negative numbers.

Example 2:

Reading a CSV record

Length 200  & Local A$ B$ C$  
Length 15.0 & Local I 
Length 15.3  & Local J K 
Length 10.10  & Local L

Fmt:  Format A$;B$;C$;I;J;K;L

Clear
Print(Et)
Close(1) & Open(1) 'xxx.Csv', Dir='cos'
File(1)Csv
Read(1,Fmt)
Print 'a$=';A$
Print 'b$=';B$
Print 'c$=';C$
Print 'i=';I
Print 'j=';J
Print 'k=';K
Print 'l=';L
Print 'done'   
Wait
Stop

The Output of this program looks like this:

a$=simple string
b$= String with embeded double "Quotation""marks
c$= String including a, comma
i=              10-
j=          10.340
k=          99.345-
l=.0001234500-
done

The contents of string variables are not padded with blanks. They contain exactly what was in the CSV field.

A note about very large or very precise numbers:

Some windows applications such as Excel, cannot tolorate very long numbers or numbers with precision as long as allowed by Comet.

In this case, they will revert to floating point notation.

As a result, these applications may cause loss of precision or Comet may not be able to read those numbers.

CSV processing was introduced in Comet 2008. File()DLM= and File()NORMAL are available in Comet32 only.

Personal tools