User Tools

Site Tools


reading_20and_20writing_20csv_20files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
reading_20and_20writing_20csv_20files [2024/01/05 00:21] – external edit 127.0.0.1reading_20and_20writing_20csv_20files [2026/02/14 13:45] (current) – Accept files with LF termination richardrussell
Line 1: Line 1:
 =====Reading and writing CSV files===== =====Reading and writing CSV files=====
  
-//by Richard Russell, July 2007//\\ \\ **Comma Separated Value** or **CSV** files are a common format typically used to represent the contents of a spreadsheet or database (or any other 2D matrix of data cells); they can by read and written by programs such as **Microsoft Excel** and **Microsoft Access**. The main properties of CSV files are as follows:\\ \\ +//by Richard Russell, July 2007//
  
-  * Data rows are separated by newlines (CRLF).+**Comma Separated Value** or **CSV** files are a common format typically used to represent the contents of a spreadsheet or database (or any other 2D matrix of data cells); they can by read and written by programs such as **Microsoft Excel** and **Microsoft Access**. The main properties of CSV files are as follows:  
 + 
 +  * Data rows are separated by newlines (CRLF or LF).
   * Data columns are separated by commas.   * Data columns are separated by commas.
   * Quotation marks must be escaped using the convention **""**.   * Quotation marks must be escaped using the convention **""**.
   * Data cells containing commas, newlines or quotes must be enclosed in quotes.   * Data cells containing commas, newlines or quotes must be enclosed in quotes.
   * Any data cell may optionally be enclosed in quotes.   * Any data cell may optionally be enclosed in quotes.
-\\  Here is an example of the contents of a CSV file:\\ \\ + 
 +Here is an example of the contents of a CSV file: 
   1997,Ford,E350,"AC, ABS, moon roof",3000.00   1997,Ford,E350,"AC, ABS, moon roof",3000.00
   1999,Chevy,"Venture ""Extended Edition""",,4900.00   1999,Chevy,"Venture ""Extended Edition""",,4900.00
-For more details see this [[http://en.wikipedia.org/wiki/Comma-separated_values|Wikipedia article]].\\ \\  The routines listed below read a CSV file into a 2D string array or write a 2D string array into a CSV file respectively. Note that spaces before an initial quote character are not permitted. If the CSV file uses a non-standard line termination (e.g. **LF** or **LFCR**) then replace the **INPUT #F%,A$** with **A$ = FNreadline(F%)** where the FNreadline function can be found in this [[/Reading%20and%20writing%20plain%20text%20files|article]].\\ \\ + 
 +For more details see this [[http://en.wikipedia.org/wiki/Comma-separated_values|Wikipedia article]]. 
 + 
 +The routines listed below read a CSV file into a 2D string array or write a 2D string array into a CSV file respectively. Note that spaces before an initial quote character are not permitted. 
 <code bb4w> <code bb4w>
         DEF PROCreadCSV(file$,array$())         DEF PROCreadCSV(file$,array$())
Line 19: Line 27:
         array$() = ""         array$() = ""
         REPEAT         REPEAT
-          INPUT #F%,A$ +        A$ = GET$#F% 
-          IF ASCA$=10:A$ = MID$(A$,2)+        IF A$="" IF PTR#F%>1 THEN PTR#F%=PTR#F%-2 IF BGET#F%<>BGET#F% A$=GET$#F%
           FOR C% = 0 TO DIM(array$(),2)           FOR C% = 0 TO DIM(array$(),2)
             I% = 1             I% = 1
reading_20and_20writing_20csv_20files.txt · Last modified: 2026/02/14 13:45 by richardrussell