Skip to content

jasonsparc/DSVParser-AHK

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DSVParser-AHK

A simple utility for reliably parsing delimiter-separated values (i.e., DSV) in AutoHotkey v1 scripts, whether that be comma-separated (i.e., CSV), tab-separated (i.e., TSV), or something else, possibly even exotic ones.

This is a small library for AutoHotkey v1. For AutoHotkey v2, check out, https://github.com/jasonsparc/dsvparser-ahk2

Also, for the motivation behind the creation of this library, check out the forum post: “[Library] DSV Parser - AutoHotkey Community

Features

  • RFC 4180 compliant.
  • Supports newlines and other weird characters in cells enclosed in text qualifiers.
  • Allows custom delimiters and text qualifiers.
  • Supports multiple delimiters (like Microsoft Excel).
  • Supports multiple qualifiers (unlike Microsoft Excel).
  • Proper support for malformed inputs (e.g., "hello" world "foo bar" will be parsed as hello world "foo bar").
    • Achieved by treating cells as composed of two components: a text-qualified part (i.e., any raw string, excluding unescaped qualifier characters), and a delimited text part (i.e., any raw string, including qualifier characters, except newlines and delimiter characters).
    • The behavior for ill-formed cells are therefore not undefined.
    • The above treatment is also similar to that of Microsoft Excel.
  • Recognizes many ASCII and Unicode line break representations:

Example

Basic usage

; Load a TSV data string
FileRead tsvStr, data.tsv

; Parse the TSV data string
MyTable := TSVParser.ToArray(tsvStr)

; Do something with `MyTable`

MsgBox % MyTable[2][1] ; Access 1st cell of 2nd row

; ... do something else with `MyTable` ...

; Convert into a CSV, with custom line break settings
csvStr := CSVParser.FromArray(MyTable, "`n", false)

FileDelete new-data.csv
FileAppend %csvStr%, *new-data.csv

And there's more!

Both TSVParser and CSVParser are premade instances of the class DSVParser. To read and write in other formats, create a new instance of DSVParser and specify your desired configuration.

Here's a DSVParser for pipe-separated values (aka., bar-separated):

global BSVParser := new DSVParser("|")

Many more utility functions are provided for parsing and formatting DSV strings, including parsing just a single DSV cell.

Check out the source code! It's really just a tiny file.