Sign in Join
Wiki copytemporary

CSV: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: CSV

OldNewLine
1+## Overview
2+
3+CSV means Comma-Separated Values, and is a popular file format that is unfortunately not completely standardized.
4+
5+It is a text based file format with
6+
7+- data fields separated by commas (or, in variants, other characters like tabs and semicolons)
8+- there may or not be a header line that lists the field names
9+- field data containing delimiters may be prohibited or enclosed by quotes (most commonly the double quote character)
10+- line endings (#13 and/or \#10) may or may not be allowed in field data
11+
12+[RFC4180](http://www.rfc-editor.org/rfc/rfc4180.txt) tries to codify and standardize existing practice; it makes sense to conform to this standard when writing CSV data (and accept all RFC4180 data when reading).
13+
14+A different, Creativyst/Excel, CSV pseudo-format specification can be found [here](http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#FileFormat).
15+
16+A sample CSV snippet:
17+
18+`FirstName,Surname,DOB,Remarks`\
19+`Jim,Weston,19560818,"Also known as ""The Butcher"""`\
20+`Alice,Cooper,19760312,""`
21+
22+Apparently a header line is used here, as is quoting using double quotes.
23+
24+Spreadsheet packages such as Microsoft Excel and OpenOffice/LibreOffice Calc are able to export to and import from this format. However, as Microsoft Excel may interpret some fields such as date fields differently depending on a user's OS locale, it may pay to find alternative ways of transferring data (e.g. using the [[FPSpreadsheet]] code).
25+
26+## CSV and SDF
27+
28+Delphi (and Free Pascal) have a very similar (but not identical) format, SDF. See [[SDF]] for more details.
29+
30+## Implementations
31+
32+### CsvDocument
33+
34+[[CsvDocument]] is a robust implementation of both the RFC 4180 CSV format and the alternative Creativyst/Excel CSV format. It offers both line-based and document-based access. Recommended for use with FPC/Lazarus. See [[CsvDocument]].
35+
36+### DelimitedText
37+
38+TStringList offers the DelimitedText property. This parses a line of text out into separate fields. Note, however, that DelimitedText is supposed to be in SDF, a Delphi-specific format that is very much like CSV, but does not conform with RFC4180 completely.
39+
40+Tips: when reading CSV data, set the *StrictDelimiter* property to true.
41+
42+When writing out CSV data, set *StrictDelimiter* to false and output the *DelimitedText* property. One oddity is that e.g. tab characters are removed when writing out data using *StrictDelimiter:=false*
43+
44+#### SDF format
45+
46+See [[SDF|SDF format]]
47+
48+### SDFDataset
49+
50+Free Pascal offers the [[TSdfDataSet]], which <s>stores data in SDF format.</s> Note: FPC 2.6.x and earlier store sdfdataset data in a format that is not completely CSV or completely SDF compatible.
51+
52+As indicated, [[SDF]] differs from [[CSV]]. Depending on the flavour of CSV, this format may be close enough to what a reading application expects to function. If you need an RFC4180 compliant CSV format, use [[TCSVDataSet]] instead.
53+
54+#### TSdfDataset and TFixedDataset sample files
55+
56+Sample SDF file
57+
58+
59+Below is a sample database for TSdfDataset. Note that the first line has the names of the fields and that we are using commas as separators:
60+
61+`ID,NAMEEN,NAMEPT,HEIGHT,WIDTH,PINS,DRAWINGCODE`\
62+`1,resistor,resistor,1,1,1,LINE`\
63+`2,capacitor,capacitor,1,1,1,LINE`\
64+`3,transistor npn,transistor npn`
65+
66+- Sample TFixedDataset file
67+
68+
69+Each record occupies a fixed amount of space, and if the field is smaller, spaces should be used to fill the remaining size.
70+
71+`Name = 15 chars; Surname = 15 chars; e_mail = 20 chars;`\
72+`Piet           Pompies                  piet@pompies.net`
73+
74+</pre>
75+
76+#### Using the datasets: example code
77+
78+Sometimes it is useful to create the dataset and work with it completely in code, and the following code will do exactly this. Note some peculiarities of TSdfDataset/TFixedDataset:
79+
80+- The lines in the database can have a maximum size of about 300. A fix is being researched.
81+- It is necessary to add the field definitions. Some datasets are able to fill this information alone from the database file.
82+- One should set FirstLineAsSchema to true, to indicate that the first line includes the field names and positions.
83+- The Delimiter property holds the separator for the fields. It will not be possible to use this char in strings in the database. Similarly it will currently not be possible to have line endings in the database because they mark the change between records. It's possible to overcome this by substituting the needed comma or line ending with another rarely used char, like \#. When showing the data on screen all \# chars could be converted to line endings and the inverse when storing data back to the database. The ReplaceString routine is useful here.
84+
85+``` pascal
86+uses sdfdata, db;
87+
88+constructor TComponentsDatabase.Create;
89+var
90+ FDataset: TSdfDataset;
91+begin
92+ inherited Create;
93+
94+ FDataset := TSdfDataset.Create(nil);
95+ FDataset.FileName := vConfigurations.ComponentsDBFile;
96+ FDataset.FileMustExist := false; //don't require existing csv file
97+
98+ // Not necessary with TSdfDataset:
99+ // FDataset.TableName := STR_DB_COMPONENTS_TABLE;
100+ // FDataset.PrimaryKey := STR_DB_COMPONENTS_ID;
101+
102+ // Adds field definitions
103+ FDataset.FieldDefs.Add('ID', ftString);
104+ //schema addition is needed so that sdfdataset writes out the csv header correctly:
105+ FDataset.Schema.Add('ID');
106+ FDataset.FieldDefs.Add('NAMEEN', ftString);
107+ FDataset.Schema.Add('NAMEEN');
108+ FDataset.FieldDefs.Add('NAMEPT', ftString);
109+ FDataset.Schema.Add('NAMEPT');
110+ FDataset.FieldDefs.Add('HEIGHT', ftString);
111+ FDataset.Schema.Add('HEIGHT');
112+ FDataset.FieldDefs.Add('WIDTH', ftString);
113+ FDataset.Schema.Add('WIDTH');
114+ FDataset.FieldDefs.Add('PINS', ftString);
115+ FDataset.Schema.Add('PINS');
116+ FDataset.FieldDefs.Add('DRAWINGCODE', ftString);
117+ FDataset.Schema.Add('DRAWINGCODE');
118+
119+ // Necessary for TSdfDataset
120+ FDataset.Delimiter := ',';
121+ FDataset.FirstLineAsSchema := True;
122+
123+ FDataset.Active := True;
124+
125+ // Sets the initial record
126+ CurrentRecNo := 1;
127+ FDataset.First;
128+end;
129+```
130+
131+When using [[TSdfDataSet]] directly be aware that RecNo, although it is implemented, does not work as a way to move through the dataset whether reading or writing records. The standard navigation routines like First, Next, Prior and Last work as expected, so you need to use them rather than RecNo.
132+
133+If you are used to using absolute record numbers to navigate around a database you can implement your own version of RecNo. Declare a global longint variable called CurrentRecNo which will hold the current RecNo value. Remember that this variable will have the same convention as RecNo, so the first record has number 1 (it is not zero-based). After activating the database initialize the database to the first record with TSdfDataset.First and set CurrentRecNo := 1.
134+
135+Example code:
136+
137+``` pascal
138+{@@
139+ Moves to the desired record using TDataset.Next and TDataset.Prior
140+ This avoids using TDataset.RecNo which doesn't navigate reliably in any dataset.
141+
142+ @param AID Indicates the record number. The first record has number 1
143+}
144+procedure TComponentsDatabase.GoToRec(AID: Integer);
145+begin
146+ // We are before the desired record, move forward
147+ if CurrentRecNo < AID then
148+ begin
149+ while (not FDataset.EOF) and (CurrentRecNo < AID) do
150+ begin
151+ FDataset.Next;
152+ FDataset.CursorPosChanged;
153+ Inc(CurrentRecNo);
154+ end;
155+ end
156+ // We are after the desired record, move back
157+ else if CurrentRecNo > AID then
158+ begin
159+ while (CurrentRecNo >= 1) and (CurrentRecNo > AID) do
160+ begin
161+ FDataset.Prior;
162+ FDataset.CursorPosChanged;
163+ Dec(CurrentRecNo);
164+ end;
165+ end;
166+end;
167+```
168+
169+### Data Export
170+
171+Free Pascal/Lazarus database export functionality (e.g. TCSVExporter on the Data Export tab - requires installation of the [[lazdbexport]] package) offers CSV export functionality for [[TDataSet|datasets]].
172+
173+### Jan's CSV components
174+
175+See [[JCSV (Jans CSV Components)]].
176+
177+### ZMSQL
178+
179+[[ZMSQL]] stores data in semicolon-delimited files (using SDF?).
180+
181+---
182+*From the Free Pascal wiki, page "CSV", as it stood on 2020-07-18 after a change by FTurtle, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history