Sign in to follow this  
Followers 0
Stilgar

_CSV2Array

6 posts in this topic

#1 ·  Posted (edited)

A simple function that loads a CSV-file into a 2-dim array:

; Name ..........: _CSV2Array
; Description ...:
; AutoIt Version : V3.3.0.0
; Syntax ........: _CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])
; Parameter(s): .: $hFile       - Handle for the CSV file to Read
;                  $cSeperator  - Optional: (Default = "auto") : Tries to find the separator char (; or , or TAB or | or space)
;                               | Data-seperator-char
;                               | Empty-string = Opt("GUIDataSeparatorChar")
;                  $bFilterString - Optional: (Default = True) : Removes leading and trailing " or '
;                  $iColumnMode - Optional: (Default = 0) :
;                               | 0: Sets error if lines have different columns and @extended to the csv-line number
;                               | 1: returns lines with different columns numbers comparing to the first line, too
;                               | 2: removing all columns > column numbers in the first line
; Return Value ..: Success      - 2-dim Array
;                  Failure      - 0
;                  @ERROR       - 1: error file read
;                  @ERROR       - 2: different number of columns / @EXTENDED = CSV-line
;                               - 3: parameter error
; Author(s) .....: Thorsten Willert
; Date ..........: Mon Dec 07 18:59:46 CET 2009
_CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])

Download: _CSV2Array.au3

Edited by Stilgar

Share this post


Link to post
Share on other sites



Funny: I'm currently writing my own _CsvFromArray and _CsvToArray.

Yet it departs from the guidelines cited above in that it handles Nulls and supports AutoIt and SQLite types as much as possible, e.g.:

123,0.5,"abc""def",,0x616263646566676869

will be interpreted as:

integer 123

double 0.5

string 'abc"def'

Null

Binary 0x616263646566676869

In the CSV, every string is Unicode (UTF8 in CSV) and must be within double quotes. An empty field is a Null.

Integer, double and binary have the correct type.

The goal is to offer a convenient support with SQLite as well.

In _CsvFromArray, types will be converted when it makes sense:

string, integers, floats, binary go verbatim

boolean->{0,1}

pointers and hWnd are converted to their numeric value (whatever meaning that may have)

other types are '<invalid>'


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

It sounds interesting. The wiki article points out that there is no clear definition of csv format. I believe most programs that use csv should be able to interpret files which use the suggested standardization. What you are creating goes above and beyond basic csv parsing.

Edited by czardas

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Yes but a minimum beyond. Perserving basic types is essential (e.g. 123 vs. "123"). The possibility to round-trip between AutoIt array, CSV file and SQLite table while preserving strings, numbers, binary and nulls would be interesting.

Also I don't put stringent rules on the contents: every field/cell can be any type, just like AutoIt uses flexible types and SQLite uses dynamic typing (very close to what we are used to with AutoIt). But I insist on consistent number of fields in CSV, else things become fuzzy and need two passes.

Local $aTest[2][7] = [ _
    [123, 123.456, "abc""def", Binary("grhoghniogrhnio"), Null, Ptr(123456), WinWait("[REGEXPTITLE:(?i)(.*SciTE.*)]")], _
    ["123", Null, Binary("grhoghniogrhnio"), "abc""def", Null, Ptr(123456789), WinWait("[REGEXPTITLE:(?i)(.*Firefox.*)]")] _
]

ConsoleWrite(_CsvFromArray($aTest, 0) & @LF)

Produces:
123,123.456,"abc""def",0x6772686F67686E696F6772686E696F,,0x0001E240,0x00300C32
"123",,0x6772686F67686E696F6772686E696F,"abc""def",,0x075BCD15,0x000406F6

I realize I'm just hijacking the thread. Please Stigard, accept my apologies. I'll create a separate thread later.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0