CSV Table Functions - ReadTable(), WriteTable(), SortTable()

Post your working scripts, libraries and tools
User avatar
berban
Posts: 85
Joined: 14 Apr 2014, 03:20

CSV Table Functions - ReadTable(), WriteTable(), SortTable()

13 Dec 2018, 00:04

I have these simple functions for dealing with CSV table files and I thought I'd share them here. I'm sure others might have similar functions but here's a few more.

These functions are built to handle cells with multiple lines or literal commas. The resulting tables are treated as 2-dimensional AutoHotkey array objects.

GitHub: https://github.com/berban/Tables/blob/master/Tables.ahk

There are 3 functions, one each for reading, writing, and sorting tables. I put some simple explanations in the code above each function. If you need more clarification then don't hesitate to ask in this thread.

Simple usage example:

Code: Select all

#Include, Tables.ahk

outputArray := ReadTable("C:\myFile.csv")      ; read the entire file
MsgBox, % outputArray[2][3]                    ; shows the contents of the cell at row 2, column 3
outputArray[2][3] := "new value"               ; store a new value to that cell
WriteTable(outputArray, "C:\myFile.csv")       ; overwrite the original csv file with the updated table

;------------------------------------------------------------------------------------------------------------------------

outputArray := ReadTable("C:\myFile.csv", {"Headers" : True}, columnNames)      ; read the entire file but treat the first row as column headers. The second row in the file is now treated as the first row of data. The names of the columns is stored in the variable %columnNames%
MsgBox, % outputArray[2]["Product ID"]                                          ; shows the contents of the "Product ID" column for the 2nd entry in the table (which is the 3rd line of the csv file)
outputArray[2]["Product ID"] := "new value"                                     ; store a new value
WriteTable(outputArray, "C:\myFile.csv", columnNames)                           ; overwrite the original csv file with the updated table. It's important to include the columnNames variable so the columns are written in the same order as they were in the original file, otherwise they will be written in alphabetical order.

;------------------------------------------------------------------------------------------------------------------------

tableString =                                                     ; the function can also be used to quickly parse a string into a 2-dimensional array
(
Row1	123	abc
Row2	456	def
Row3	789	ghi
)
outputArray := ReadTable(tableString, {"Delimiter" : A_Tab})      ; in this case the delimiter is a tab. default is comma
MsgBox, % outputArray[2][3]                                       ; shows "def", the contents of the 2nd row 3rd column
Last edited by berban on 23 Jan 2019, 23:55, edited 3 times in total.
DRocks
Posts: 448
Joined: 08 May 2018, 10:20

Re: CSV Table Functions - ReadTable(), WriteTable(), SortTable()

13 Dec 2018, 07:59

Seems Nice thank you very much :)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: arcticir, cyruz and 36 guests