Sign in to follow this  
Followers 0
frankhack1

How to? Read CSV Specific Row and Columns?

7 posts in this topic

#1 ·  Posted (edited)

Hi !

I have a .csv with 6 columns and 31 rows.

I need a script to read a .csv file from the beginning of the Column 5 and from the Row 2 (since the row 1 is the title I don't want to use that info) and all the info that is on Column 5 to Send each data and then {{TAB 2}} and send next data and {{TAB 2}} and so on till the end of the rows in Column 5.

THe script should only move from beginning to end on all the rows in column 5.

THis is what I have:

#include <GUIConstants.au3>
#include <string.au3>

; FUNCTION TO RECOGNIZE THE GOOGLE CHROME WINDOW
Func Chrome ()
AutoItSetOption('WinTitleMatchMode', 2)
$tbWin = 'Chrome'
WinActivate($tbWin)
EndFunc

' OPEN OR READ CSV FILE
$file = FileOpen("C:\Users\admin\Desktop\Dropbox\iMacros\Datasources\01_PISOS\Precios\Espana\Barcelona\NEW\Diciembre\NH_01\01.csv", 0)

If $file = -1 Then
   MsgBox(0, "error", "File doesn't exist or can't be read")
   Exit
EndIf

   ; Read in lines of text until the EOF is reached
While 1
    Local $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
   $input = StringSplit($line, ",")

Local $value1 = $input[5]

Send($value1)

WEnd
 

This is the content that I have on the .csv file:

"Day","Fecha","Month","Year","Estandar","Tarifa"
"L","1","12","2014","15","<SP>"
"M","2","12","2014","15","<SP>"
"X","3","12","2014","15","<SP>"
"J","4","12","2014","15","<SP>"
"V","5","12","2014","20","<SP>"
"S","6","12","2014","20","<SP>"
"D","7","12","2014","15","<SP>"
"L","8","12","2014","15","<SP>"
"M","9","12","2014","15","<SP>"
"X","10","12","2014","15","<SP>"
"J","11","12","2014","15","<SP>"
"V","12","12","2014","20","<SP>"
"S","13","12","2014","20","<SP>"
"D","14","12","2014","15","<SP>"
"L","15","12","2014","15","<SP>"
"M","16","12","2014","15","<SP>"
"X","17","12","2014","15","<SP>"
"J","18","12","2014","15","<SP>"
"V","19","12","2014","15","<SP>"
"S","20","12","2014","20","<SP>"
"D","21","12","2014","20","<SP>"
"L","22","12","2014","15","<SP>"
"M","23","12","2014","15","<SP>"
"X","24","12","2014","15","<SP>"
"J","25","12","2014","15","<SP>"
"V","26","12","2014","20","<SP>"
"S","27","12","2014","20","<SP>"
"D","28","12","2014","15","<SP>"
"L","29","12","2014","25","<SP>"
"M","30","12","2014","25","<SP>"
"X","31","12","2014","25","<SP>" 

And on Google Chrome I already have the website where I want the data to go, but I'm getting all this in one whole line:

"Estandar""15""15""15""15""20""20""15""15""15""15""15""20""20""15""15""15""15""15""15""20""20""15""15""15""15""20""20""15""25""25""25"

How can I avoid the 1st row "Estandar"

How can I take out the "" and just keep the data

How can I Send data then {{TAB 2}} Send next data then {{TAB 2}}

This an screenshot of where the data will go, basically, it should put a price on the each square and then send TAB twice to go to next square and put next data pulled from csv

10858550_10205163411465960_3139116407224

Frank

Edited by frankhack1

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You somehow managed to explain smth very simply very complicated.

Not sure I got you, but here's an example how to read your CSV. Uncomment commented string to read specific row and column and show it in dialog box.

#include <Array.au3>

Global $a = ReadCSV ("D:\Temp\Test.csv")
_ArrayDisplay ($a)
;MsgBox (0, "", StringReplace ($a[4][3], """", ""))

Func ReadCSV ($p_csv_file)
  Local $file = FileOpen ($p_csv_file)
  If $file = -1 Then  Return SetError (1, 0, 0)

  Local $s = FileRead ($file)
  FileClose ($file)

  Return StringToArray ($s, false, ",")
EndFunc

Func StringToArray ($p_string, $p_transpose_1d = false, $p_delim_col = "|", $p_delim_row = @CRLF, $p_arr_delim_col = Chr (31), $p_arr_delim_row = Chr (30))
  Local $array, $rows, $cols, $last_row, $last_col

  $rows = StringSplit ($p_string, $p_delim_row, 3)
  $last_row = UBound ($rows) - 1

  If $last_row < 1 Then
    ;Array rows = 0
    $cols = StringSplit ($p_string, $p_delim_col, 3)
    $last_col = UBound ($cols) - 1

    If $last_col < 1 Then
      ;Array rows = 0, columns = 0
      Dim $array[1]
      If StringInStr ($p_string, $p_arr_delim_col) or StringInStr ($p_string, $p_arr_delim_row) Then
        $array[0] = StringToArray ($p_string, $p_transpose_1d, $p_arr_delim_col, $p_arr_delim_row)
      Else
        $array[0] = $p_string
      EndIf
    Else
      ;Array rows = 0, columns > 0
      Dim $array[$last_col + 1]
      For $i = 0 To $last_col
        If StringInStr ($cols[$i], $p_arr_delim_col) or StringInStr ($cols[$i], $p_arr_delim_row) Then
          $array[$i] = StringToArray ($cols[$i], $p_transpose_1d, $p_arr_delim_col, $p_arr_delim_row)
        Else
          $array[$i] = $cols[$i]
        EndIf
      Next
    EndIf
    If $p_transpose_1d Then _ArrayTranspose ($array)
  Else
    ;Array rows > 0
    $last_col = 0
    For $i = 0 To $last_row
      StringReplace ($rows[$i], $p_delim_col, "")
      If @extended > $last_col Then $last_col = @extended
    Next

    If $last_col < 1 Then
      ;Array rows > 0, columns = 0
      Dim $array[$last_row + 1]
      For $i = 0 To $last_row
        If StringInStr ($rows[$i], $p_arr_delim_col) or StringInStr ($rows[$i], $p_arr_delim_row) Then
          $array[$i] = StringToArray ($rows[$i], $p_transpose_1d, $p_arr_delim_col, $p_arr_delim_row)
        Else
          $array[$i] = $rows[$i]
        EndIf
      Next
    Else
      ;Array rows > 0, columns > 0
      Dim $array[$last_row + 1][$last_col + 1]
      For $i = 0 To $last_row
        $cols = StringSplit ($rows[$i], $p_delim_col, 3)
        If $last_col > UBound ($cols) - 1 Then ReDim $cols[$last_col + 1]
        For $n = 0 To $last_col
          If StringInStr ($cols[$n], $p_arr_delim_col) or StringInStr ($cols[$n], $p_arr_delim_row) Then
            $array[$i][$n] = StringToArray ($cols[$n], $p_transpose_1d, $p_arr_delim_col, $p_arr_delim_row)
          Else
            $array[$i][$n] = $cols[$n]
          EndIf
        Next
      Next
    EndIf
  EndIf

  If not IsString ($p_string) Then SetError (1)
  If $p_string = "" Then SetError (2)

  Return $array
EndFunc
Edited by mjolnirmarkiv

Share this post


Link to post
Share on other sites

I use this >UDF to parse a CSV file into an array.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

frankhack1 and water,

_FileReadToArray will parse a CSV file into an array from v3.3.12.0 on... :whistle:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Ops. Seems I'm not up to date  :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

HI !

With the other scripts that you gave me, I couldn't understanded it.

But mine is more or less I got what I want, but I need to send TAB twice in order to jump to next box, please check video and you will see that all the info extracted from the .csv is going to the same place and this is wrong = 

This is how it should work = 

What I'm missing is the TAB part, also I want to start reading from the 2 line since the 1st line are the titles.

My code modified is this:

#include <GUIConstants.au3>
#include <string.au3>

; FUNCTION TO RECOGNIZE THE GOOGLE CHROME WINDOW
Func Chrome ()
AutoItSetOption('WinTitleMatchMode', 2)
$tbWin = 'Chrome'
WinActivate($tbWin)
EndFunc

; OPEN OR READ CSV FILE
$file = FileOpen("c:/01.csv", 0)

If $file = -1 Then
   MsgBox(0, "error", "File doesn't exist or can't be read")
   Exit
EndIf

   ; Read in lines of text until the EOF is reached
While 1
    Local $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
   $input = StringSplit($line, ",")

Local $value1 = $input[5]

$aCsvData = StringReplace($value1, """", "")
Call("Chrome")
Send($aCsvData)

WEnd
Edited by frankhack1

Share this post


Link to post
Share on other sites

Of course you can easily extract the col (without title) using something like this

#Include <Array.au3>
$txt = FileRead("1.csv")
$res = StringRegExp($txt, '(?m)^(?:.*?,){4}"(\d+)', 3)
 _ArrayDisplay($res)

Sending to browser is another story  :)

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