intro.bbdoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Text.CSV is a CSV (Comma Separated Values) file parser. CSV files are a popular format for exchanging data between
  2. applications, as they are simple, flexible, and can be easily generated or consumed by various software programs.
  3. We will explore the CSV file format in depth, and learn how to use the Text.CSV to load and manipulate CSV files in
  4. BlitzMax applications.
  5. ## CSV File Format
  6. CSV files are plain text files that store tabular data, making them suitable for representing spreadsheet-like
  7. structures. Each row in a CSV file corresponds to a single line of text, and columns within a row are separated by
  8. a delimiter, which is typically a comma (`,`) or a tab (`\t`). CSV files can store various types of data, such as
  9. numbers, text, and dates, and they can accommodate both simple and complex data structures.
  10. There are several reasons why one might choose to use the CSV file format over other formats:
  11. 1. **Simplicity** : CSV files are easy to create and understand, as they have a straightforward structure and use plain
  12. text encoding.
  13. 2. **Portability** : CSV files can be read and written by a wide variety of software applications, including
  14. spreadsheet programs, databases, and data analysis tools.
  15. 3. **Size** : Because CSV files are plain text, they can be compressed efficiently, reducing storage and
  16. transmission costs.
  17. 4. **Human Readable** : Unlike binary formats, CSV files can be opened and examined with a simple text editor,
  18. making it easier to identify and troubleshoot issues.
  19. To create a valid CSV file that can be loaded into Text.CSV, you should follow these basic rules:
  20. 1. **Consistent Delimiters** : Use the same delimiter character (e.g., a comma or a tab) to separate columns
  21. throughout the entire file. Mixing delimiters within the same file can lead to parsing errors.
  22. 2. **Quoting** : If a field value contains the delimiter character, newlines, or double quotes, enclose the value
  23. in double quotes. For example, `"John Doe", 25, "123 Main St, Apt 4B"` has the address field quoted because it contains
  24. a comma. To represent a double quote within a quoted field, use two double quotes together, like `""`. For example,
  25. `"He said, ""Hello"""`
  26. 3. **Header Row** : It's a good practice to include a header row at the beginning of the file that lists the column
  27. names. This makes it easier to understand the structure and content of the file.
  28. 4. **Consistent Rows** : Ensure that each row has the same number of columns. This helps prevent parsing errors and
  29. ensures that the data can be processed correctly.
  30. 5. **UTF-8 Encoding** : Save the file using UTF-8 encoding to ensure that it can be read correctly by the parser.
  31. ## Parsing CSV Files
  32. You'll need to create an instance of the #TCsvParser type, and then use its various methods to interact with the
  33. CSV data. The parser accepts input streams in UTF-8 format.
  34. Here is a basic example of how to use the module:
  35. ```blitzmax
  36. SuperStrict
  37. Framework BRL.StandardIO
  38. Import Text.CSV
  39. Local options:TCsvOptions = New TCsvOptions
  40. options.delimiter = ","
  41. Local parser:TCsvParser = TCsvParser.Parse("path/to/your/csvfile.csv", options)
  42. While parser.NextRow() = ECsvStatus.row
  43. Local row:TCsvRow = parser.GetRow()
  44. Print row.GetValue("column_name")
  45. Wend
  46. parser.Free()
  47. ```