Jump to content

csv udf


ivan
 Share

Recommended Posts

Here's a udf to handle csv files.

I'm releasing the latest version _CSVLib_V1.3.au3, with nearly 10 new functions, all functions have been thoroughly reviewed and tested. I found a minor bug in _CSVConvertLineBreak, and I'll fix it soon.

The examples do not work with version 1.3, but a single file will be made available in the next revision in mid february.

I'm cought up in the middle of an install kit that I was hired for, so I really can't commit myself earlier than that.

All versions prior to 1.3 and their respective examples will be removed in that revision.

I'd really appreciate any help testing these functions or comments or ideas.

Thanks in advance.

IVAN

CSVExample.au3

_CSVLib.au3

_CSVLib_V1.0.au3

CSVExampleFile_V1.au3

_CSVLib_V1.1.au3

CSVGuiExample_V1.1.au3

_CSVLib_V1.3.au3

Edited by ivan
Link to comment
Share on other sites

;===========================================================================================

; Function List:

; _CSVGetRecords: Retreives the contents of a csv file to a 1 Dim Array.

; _CSVGetColumn: Retreives the column of a csv file to a 1 Dim Array.

; _CSVRecordToFields: Converts a given Record (csv line) to a 1 Dim array of Fields

; _CSVStringToField: Converts a text string to csv format to store in a single field.

; _CSVFieldToString: Converts the content of a single csv field to a text string.

; Author: IVAN PEREZ

;===========================================================================================

i dont see this function in the list above

;===========================================================================================

; Function Name: _CSVGetField

............

????

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks for the encouragement guys.

I've got about 10 changes to the previous scripts including functions that specifically convert delimiters and enclose characters, as well as writing csv. I'll make them available as soon as I get back to the office in early January.

@Valuater:

Thanks. I added _CSVGetField to the list and updated the original post.

However, those are likely to be changed as I'll be including CSV write functions as well as delimiter and enclose conversions. I'll also improve the example and provide some documentation on the processes used. It's an honor that you've taken a look at my script.

I just lost my number of downloads. Shame.

Regards and merry crhistmas.

IVAN

Edited by ivan
Link to comment
Share on other sites

be sure to check this one also...

;===========================================================================================
; Function Name:     _CSVFieldToString
; Description:        Converts the content of a single csv field to a text string.
; Syntax:           _CSVFieldToString($pRawField [,$pEnclose] [,$pDelimiter])
; Parameter(s):     $pRawField    -    Raw csv string.
;                    $pDelimiter    -    Use -1 for default field separator char ','.
;                    $pEnclose    -    Use -1 for default quote char '"'.
; Requirement(s):   None
; Return Value(s):  On Success    -    1 Dim $lRecords array
;                    On Failure    -    Errors not defined yet
; Warning:            Not tested with Delimiters other than the defaults.
; Date:                20/12/06
; Author:            IVAN PEREZ
;===========================================================================================
Func _CSVFieldToString($pRawField, $pDelimiter, $pEnclose)
    Local $lStr = $pRawField
    ; strip StartEnclose and EndEnclose
    If StringLeft($lStr, 1) = $pEnclose And StringRight($lStr, 1) = $pEnclose Then
        $lStr = StringRight($lStr, StringLen($lStr) - 1)
        $lStr = StringLeft($lStr, StringLen($lStr) - 1)
        $lStr = StringReplace($lStr, $pEnclose & $pEnclose, $pEnclose)
    EndIf
    Return $lStr
EndFunc   ;==>_CSVFieldToString

where...

$pDelimiter - Use -1 for default field separator char ','.

is not used within the function

.... keep up the good work

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks again Valuater.

I've implemented your suggestion in _CSVLib_V1.0.au3.

This modified script with about a dozen new functions ought to work...

Some functions have not even been tested, but they rely on the same principle.

I'd be really grateful if you or anyone else can give me a hand testing them, as I'm away and have little time to look at the script.

IVAN

be sure to check this one also...

;===========================================================================================
; Function Name:     _CSVFieldToString
; Description:        Converts the content of a single csv field to a text string.
; Syntax:           _CSVFieldToString($pRawField [,$pEnclose] [,$pDelimiter])
; Parameter(s):     $pRawField    -    Raw csv string.
;                    $pDelimiter    -    Use -1 for default field separator char ','.
;                    $pEnclose    -    Use -1 for default quote char '"'.
; Requirement(s):   None
; Return Value(s):  On Success    -    1 Dim $lRecords array
;                    On Failure    -    Errors not defined yet
; Warning:            Not tested with Delimiters other than the defaults.
; Date:                20/12/06
; Author:            IVAN PEREZ
;===========================================================================================
Func _CSVFieldToString($pRawField, $pDelimiter, $pEnclose)
    Local $lStr = $pRawField
    ; strip StartEnclose and EndEnclose
    If StringLeft($lStr, 1) = $pEnclose And StringRight($lStr, 1) = $pEnclose Then
        $lStr = StringRight($lStr, StringLen($lStr) - 1)
        $lStr = StringLeft($lStr, StringLen($lStr) - 1)
        $lStr = StringReplace($lStr, $pEnclose & $pEnclose, $pEnclose)
    EndIf
    Return $lStr
EndFunc   ;==>_CSVFieldToString

where...

$pDelimiter - Use -1 for default field separator char ','.

is not used within the function

.... keep up the good work

8)

Link to comment
Share on other sites

You cant just post... testing is needed

+> Starting AutoIt3Wrapper v.1.7.4

>Running AU3Check (1.54.5.0) params: from:C:\Program Files\AutoIt3\beta

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(582,92) : ERROR: _CSVFieldToString() called with wrong number of args.

$lFields[uBound($lFields) - 1]= _CSVFieldToString($lRecord[$j], $pDelimiter, $pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField, $pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(596,64) : ERROR: _CSVFieldToString() called with wrong number of args.

$lField = _CSVFieldToString($pConvert, $pDelimiter, $pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField, $pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(649,92) : ERROR: _CSVFieldToString() called with wrong number of args.

$lFields[uBound($lFields) - 1] = _CSVFieldToString($lRecord[$j], $pDelimiter, $pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField, $pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(663,64) : ERROR: _CSVFieldToString() called with wrong number of args.

$lField = _CSVFieldToString($pConvert, $pDelimiter, $pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField, $pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3 - 4 error(s), 0 warning(s)

!>AU3Check ended.rc:2

>Exit code: 0 Time: 0.518

8)

NEWHeader1.png

Link to comment
Share on other sites

That was quick.

When I deleted the unecessary $pDelimiter in _CSVFieldToString, I forgot to feed the changes into the funcs that called it.

It's taken care of and the correction has been uploaded in the original post.

IVAN

You cant just post... testing is needed

8)

Edited by ivan
Link to comment
Share on other sites

  • 2 weeks later...

Fixes on conversion functions in _CSVLib_V1.1 and new gui example in CSVGuiExample_V1.1 available in first post.

New example should prove that any 2 chars can be used as delimiter and enclose respectively.

Please provide feedback.

IVAN

Edited by ivan
Link to comment
Share on other sites

  • 2 weeks later...

I recently got a PM with a question about which version matches which example, so I apologize for the potential confusion.

You should be able to tell from the include line on the example and download the library required.

Version 1.3 does not have an associated example file, and there were significant changes to existing functions, not only to names, but the way they work, namely using ByRef in function parameters so as to speed up processes and reduce the amount of coding required to accomplish a task.

I will make an example file for version 1.3 in mid february, as soon as I finish a job I'm doing, and there's a small bug in line the break conversion function which I need to fix.

Once that is done, I WILL REMOVE ALL PREVIOUS VERSIONS AND LEAVE A SINGLE VERSION, BASED ON 1.3.

2 Reasons for this,

1) I'm running out of space

2) I don't want to create confusion

Regards

IVAN

Link to comment
Share on other sites

Looks nice...

1

Yes, an example would help and removal of the older versions is a must

2

I noticed an error return in an incorrect order (7)

If Not _CSVColumnAppend($lRecords, $pColumn, $pDelimiter, $pEnclose, $pMode) Then
        SetError(7, @error)*******************Here**
        Return 0
    EndIf
    ; open file in overwrite mode
    $lCSVFileHandle = FileOpen($pFullPath, 2)
    If $lCSVFileHandle = -1 Then
        FileClose($lCSVFileHandle)
        SetError(5)
        Return 0
    EndIf
    For $i = 1 To UBound($lRecords) - 1 Step 1
        $lCheckWrite = FileWriteLine($lCSVFileHandle, $lRecords[$i])
        If $lCheckWrite = 0 Then
            FileClose($lCSVFileHandle)
            SetError(6)
            Return 0
        EndIf
 oÝ÷ Ûr'¢Øy«mg¬Ê®±ä´J뢽^®º+ëf¢ËeÊ;¬µ'­ºè¯R-)äÛ ¢m¡©e¶Ü¢Z+£M4ÓM,¥©ì·)^oÝ÷ Ø*.ÖÞjëh×6
If Not _CSVColumnAppend($lRecords, $pColumn, $pDelimiter, $pEnclose, $pMode) Then
        Return SetError(7, @error, 0)

Less code also!

..... Just some thoughts

I notice all the views and downloads but nobody seems to post any thoughts ( same with my threads )

lol

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

@Valuater:

I'll certainly add the error setting/tracking as one of the top todos in my next revision in mid february.

Whenever I use @error in SetError it's mainly for back tracking purposes, so I'll strictly limit to that.

I guess that could make it easier to read.

Why didn't I think of Return SetError(7, @error, 0) ??? I think it's great!

I was also surprised to see that, except from the first reply and your greatly appreciated comments, no one else has posted anything. I believe that all the files are creating confusion, and now with the upgrades in version 1.3, confusion is likely to be even greater.

Hopefully there'll be more comments once people see it working in a more real example. I'm working on one that uses the driverquery command in cmd to output a csv file. Then I try to illustrate various functions with the driver query results. I still have some work to do on it, but so far it looks promising.

Thanks,

IVAN

Link to comment
Share on other sites

  • 5 weeks later...

Sorry for the delay, and it's gonna take longer.

My daughter cought pneumonia over carnival and was hospitalized. She left the intensive care unit yesterday (over 4 days of traumatic experience for a 2 year old), so don't expect me around for a while.

IVAN

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

Here's a udf to handle csv files.

I'm releasing the latest version _CSVLib_V1.3.au3, with nearly 10 new functions, all functions have been thoroughly reviewed and tested. I found a minor bug in _CSVConvertLineBreak, and I'll fix it soon.

The examples do not work with version 1.3, but a single file will be made available in the next revision in mid february.

I'm cought up in the middle of an install kit that I was hired for, so I really can't commit myself earlier than that.

All versions prior to 1.3 and their respective examples will be removed in that revision.

I'd really appreciate any help testing these functions or comments or ideas.

Thanks in advance.

IVAN

Excellent coding.
Link to comment
Share on other sites

  • 2 weeks later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...