Jump to content

Reading lines in a csv file


Recommended Posts

Hi all

Apologies if there is allready a post to do what I am looking for. I looked arround a bit and google ect but thought it might be faster to just ask and see if someone can help or point me in the right direction. I need to read multiple lines in a CSV file and check if the fist argument "Path" exists. If it does display the values and move onto the next line ect. I have the following code allready I just need the "array/loop" to continue.

#include <GUIConstants.au3>

#include <string.au3>

#include <File.au3>

$path = "1"

$file = FileOpen("c:\test.csv", 0)

If $file = -1 Then

MsgBox(0, "error", "File doesn't exist or can't be read")

Exit

EndIf

;Loop for every line in CSV

$string = (FileReadLine($file, 1))

$input = StringSplit($string, ",", 1)

$value1 = $input[1]

$value2 = $input[2]

$value3 = $input[3]

$value4 = $input[4]

$value5 = $input[5]

If $value1 = $path Then

MsgBox(0, "Values", $value1 & " - " & $value2 & " - " & $value3 & " - " & $value4 & " - " & $value5)

Else

MsgBox(0, "Values", "Does not exist")

EndIf

Any help would be great thanks

Link to comment
Share on other sites

#include <file.au3>
#include <array.au3>

Dim $a_lines, $i, $a_temp
$s_path = 1
$rc = _FileReadToArray("C:\test.csv", $a_lines)
If $rc <> 0 Then
    For $i = 1 To $a_lines[0]
        $a_temp = StringSplit($a_lines[0], ",")
        If IsArray($a_temp) And $a_temp[0] > 0 Then
            If $a_temp[1] = $path Then
                MsgBox(0, "Values", _ArrayToString($a_temp, " - ", 1)
            EndIf
        EndIf
    Next
EndIf

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Hi Hannes

Thank you for your reply. I fiddled a bit as well in the meantime and came up with this and it does exactaly what i want. Thank you for your time though.

#include <GUIConstants.au3>

#include <string.au3>

#Include <File.au3>

$path = "1"

$FilePath = "c:\test.csv"

If Not FileExists($FilePath) Then

MsgBox(0, "error", "File doesn't exist or can't be read")

Exit

EndIf

For $count = 1 to _FileCountLines($FilePath) Step 1

$string = FileReadLine($FilePath, $count)

$input = StringSplit($string, ",", 1)

$value1 = $input[1]

$value2 = $input[2]

$value3 = $input[3]

$value4 = $input[4]

$value5 = $input[5]

If $value1 = $path Then

MsgBox(0, "Values", $value1 & " - " & $value2 & " - " & $value3 & " - " & $value4 & " - " & $value5)

Else

MsgBox(0, "Values", "Does not exist")

EndIf

Next

Regards

Mark

Link to comment
Share on other sites

Hi Mark,

what you are coding here might work :) but let me add two or three notes.

- Using _FileCountLines() reads the whole file

- Check the @error status after FileReadLine, if it is -1 then you've read the end of the file

- You don't need to reassign the single array elements to variables (e.g. $valueX = $input[X] you can just use $input[X] in your MsgBox.

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

  • 4 years later...

Here is the finished code that I got to work perfectly.  Thanks for all the help again

 

 
#include <File.au3>
#include <Array.au3>

Global $aCSV[1]
Global $intCount = 0
Global $intRow = 0
Global $intCol = 0
Global $intLineCount = 0

_FileReadToArray("C:\Test Folder\Test CSV.csv", $aCSV, Default, ",")
$intLineCount = _FileCountLines("C:\Test Folder\Test CSV.csv") - 1

While $intCount <= $intLineCount

    $intCount = $intCount + 1
    ;$intRow = $intCount

    MouseClick("primary",63,16,1)
    Send($aCSV[$intRow+$intCount][$intCol])
    Send("{ENTER}")

    Sleep(1000)

    MouseClick("primary",301,16,1)
    Send($aCSV[$intRow+$intCount][$intCol+1])
    Send("{ENTER}")

    Sleep(1000)

    MouseClick("primary",532,16,1)
    Send($aCSV[$intRow+$intCount][$intCol+2])
    Send("{ENTER}")

    Sleep(1000)

WEnd


_ArrayDisplay($aCSV)

 

 

 

Edited by JohnnyDepth
Link to comment
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
 Share

  • Recently Browsing   0 members

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