Jump to content

Recommended Posts

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
  • Moderators
Posted

$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.

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.

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

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
×
×
  • Create New...