Sign in to follow this  
Followers 0
plastix

StringRegExp to parse CSV

5 posts in this topic

#1 ·  Posted (edited)

$file = FileOpen(@ScriptDir & "1.csv",0)
$string = FileReadLine($file)

$csv = StringRegExp($string, '(".*?",|.*?,|,)', 3)

For $i = 0 To UBound($csv)-1
MsgBox(0,"CSV Data",$csv[$i])
Next FileClose($file)

After a little fiddling, this should correctly parse standard CSVs, including double quoted entries. The returned array will contain a trailing ',' (comma) which you may want to strip off...

I know this has been discussed before, but I couldn't search this one out to see if it had been concluded as 'csv' is too short for search engine. Hope that helps someone :lmao:

(sorry. corrected typo & added fileclose. was just a snippet. Of course, this just reads / parses first line of CSV - can use standard code to repeat until EOF - whatever i do wont push fileclose to new line !! ;)

Edited by plastix

Share this post


Link to post
Share on other sites



$file = FileOpen(@ScriptDir & "\1.csv",0)
$string = FileReadLine($file)

$csv = StringRegExp($line, '(".*?",|.*?,|,)', 3)

For $i = 0 To UBound($csv)-1
MsgBox(0,"CSV Data",$csv[$i])
Next

After a little fiddling, this should correctly parse standard CSVs, including double quoted entries. The returned array will contain a trailing ',' (comma) which you may want to strip off...

I know this has been discussed before, but I couldn't search this one out to see if it had been concluded as 'csv' is too short for search engine. Hope that helps someone ;)

Your example is not in working condition. I've done something similar myself, but in groups...

To fix yours, you need to either get rid of FileOpen() or add a FileClose() and change $string to $line or $line to $string.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

@SmOke_N - sorry. it isn't a standalone script - sorted typo and added fileclose so should run as is...

Share this post


Link to post
Share on other sites

Another typo

NextFileClose

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You are missing a backslash in the path to the file.

Next step. Try reading through the file by using a loop like this.

#include <Constants.au3>

$handle_read = FileOpen(@ScriptDir & "\1.csv", 0)
If $handle_read <> -1 Then
    While 1
        $string = FileReadLine($handle_read)
        If @error Then ExitLoop
        $csv = StringRegExp($string, '(".*?",|.*?,|,)', 3)
        For $i = 0 To UBound($csv) - 1
            If MsgBox(0x40001, 'CSV Data', $csv[$i]) = $IDCANCEL Then
                ExitLoop 2
            EndIf
        Next
    WEnd
    FileClose($handle_read)
Else
    MsgBox(0x40030, 'CSV Data', 'File open error')
EndIf

;)

Edited by MHz

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