reading_20and_20writing_20csv_20files
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| reading_20and_20writing_20csv_20files [2024/01/05 00:21] – external edit 127.0.0.1 | reading_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// |
| - | | + | **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 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: | ||
| 1997, | 1997, | ||
| 1999, | 1999, | ||
| - | For more details see this [[http:// | + | |
| + | For more details see this [[http:// | ||
| + | |||
| + | 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$, | DEF PROCreadCSV(file$, | ||
| Line 19: | Line 27: | ||
| array$() = "" | array$() = "" | ||
| REPEAT | REPEAT | ||
| - | INPUT #F%,A$ | + | A$ = GET$#F% |
| - | IF ASCA$=10:A$ = MID$(A$,2) | + | IF A$="" |
| FOR C% = 0 TO DIM(array$(), | FOR C% = 0 TO DIM(array$(), | ||
| I% = 1 | I% = 1 | ||
reading_20and_20writing_20csv_20files.txt · Last modified: 2026/02/14 13:45 by richardrussell