reading csv file


Recommended Posts

Dear Azeotech,

after few days trying I need Your help...

How can I read the csv file an find exactly string that I am searching and when I find it then to read 2 next datas.

So, I would like to read with barcode reader one barcode and save this as string.

In my csv file with 3 columns and a lot of rows ( first column material number , second and third parameters ) I would like with script to compare my barcod that I have read and material number from first column of my csv file and when they match then to read from second and third column of the same row and then to put this two into chanels...

I tried with ReadDelim , but this is nit exactly what I want

I want to find the right data in column B and then read in the same row data from C and D kolumn...

global FileHandle

global string strIn

FileHandle = File.Open("D:\parameters.csv",1,0,0,1)

strIn = File.ReadDelim(FileHandle,-1,",",chr(0.1),3200)

A = (strIn[0][1])

B = (strIn[0][2])

C = (strIn[0][3])

D = (strIn[0][4])

E = (strIn[0][5])

...

and if my barcod nr == C

then

channel X = D

channel Y = E

File.Close(FileHandle)

Hope You understand what I am talking about...

Thanks in addvance

post-7942-1310122068_thumb.jpg

Link to comment
Share on other sites

I would use readDelim() as you did, except since your data is all numbers, you should take the result into a numeric data type instead of a string. If you want a string, you need to put ,1 at the end of the readDelim(). I'd also pass 0 instead of 3200 and get the whole file unless its huge (like over 500,000 lines or so). If that file is never changing you can just read it in once at the beginning and keep the result in a global variable.

Then you can simply use the search() function to find the row that matches. Lets say the data from readDelim() is in a variable called "dataIn", and the code you are looking for is in a variable called barCode, then:

private index = search(dataIn[][0] == barCode)

If index then = -1, it wasn't found, otherwise you can then do:

datain[index][1] to get the next column, and [index][2] to get the 3rd column, etc.

Link to comment
Share on other sites

My apologies , in the table there are also text datas , not only numbers...

global FileHandle

global string strIn

global r = 0

FileHandle = File.Open("D:\par1.csv",1,0,0,1)

strIn = File.ReadDelim(FileHandle,-1,",",Chr(0.5),0,1)

global index = search(strIn[0][r] == strBara)

//strBara == string from barcode reader ( or manual enter )

while(1)

if ( index == -1 )

r = r+1

index = search(strIn[0][r] == strBara)

else

X = strIn[0][r+1]

Z = strIn[0][r+2]

endseq(azeotech)

endif

delay(0.001)

endwhile

File.Close(FileHandle)

That is the code that finds the data in table =my barcode nr. that I am searching and then takes next two datas...

I don't know why , but when I do

strIn = File.ReadDelim(FileHandle,-1,",",Chr(0.5),0,1)

it only returns as if there is one row and more columns ( all datas from table are in one row... ??? WHY? )

I also must put in sequence what if there is no match in table...but this is no problem...

Regards

Vulic

Link to comment
Share on other sites

  • 8 months later...

Hello again...

Below is the code that that does the job of searching for my material and parameters...

Now I have problem that this searching is to long... ( in .csv file is 1000 rows and it takes 15 seconds to find material )...

Is there a chance to make this faster ?

delay(0.1)
global FileHandle_TKX2
global string strDatoteka_TKX2
global red_TKX2 = -1
global index_TKX2 = -1
global string Redovi_TKX2
global c_TKX2
global ok_TKX2
global cancel_TKX2
global string Result_TKX2

FileHandle_TKX2 = File.Open("D:\TKX\TKXrecipes.csv",1,0,0,1)
Redovi_TKX2 = File.ReadDelim(FileHandle_TKX2,1,";",Chr(10),0,1)
c_TKX2 = sum(Redovi_TKX2 != NaN())
File.Close(FileHandle_TKX2)


FileHandle_TKX2 = File.Open("D:\TKX\TKXrecipes.csv",1,0,0,1)
strDatoteka_TKX2 = File.ReadDelim(FileHandle_TKX2,-1,";",Chr(10),0,1)


while(1)
if ( index_TKX2 == -1 )
red_TKX2 = red_TKX2 + 1
if (red_TKX2 == c_TKX2)
if(V.Jezik[0] < 0.5)
Result_TKX2 = System.MessageBox(" Parametri nisu pronadjeni. Zelite li ih upisati rucno ? ", "OkCancel" )
else
Result_TKX2 = System.MessageBox(" Parameters are not found. Would You like to enter them manual ? ", "OkCancel" )
endif
S7S7_TKX_2Materijal_nije_pronadjen_Out[0] = 1
S7S7_TKX_2Materijal_pronadjen_Out[0] = 0
if (Result_TKX2 == "OK")
Component.TKX2_BV_rucno.Contents = ""
Component.TKX2_VR_rucno.Contents = ""
Page.TKX2_unos_parametara.PopupModeless(1,150,60,900,1220)
else
Component.TKX2_BCD.Contents = ""
Barcode_TKX2[0] = ""
Brzina_vrtnje_TKX2[0] = 0
Vrijeme_rada_TKX2[0] = 0
endif
endseq(TKX2)
endif
index_TKX2 = search(strDatoteka_TKX2[red_TKX2][1] == Barcode_TKX2[0])
else
Brzina_vrtnje_TKX2[0] = strDatoteka_TKX2[red_TKX2][2]
Vrijeme_rada_TKX2[0] = StrToDouble(strDatoteka_TKX2[red_TKX2][3])*60
Component.TKX2_BCD.Contents = ""
S7S7_TKX_2Materijal_nije_pronadjen_Out[0] = 0
S7S7_TKX_2Materijal_pronadjen_Out[0] = 1
beginseq(Provjera_par_granice_TKX2)
endseq(TKX2)
endif
delay(0.01)
endwhile

File.Close(FileHandle_TKX2)[/CODE]

Thanks in advance

Link to comment
Share on other sites

I can't tell completely from your script, but basically the slow part is going to be the loop, especially since you have a delay(0.1) in there. Do what you can to eliminate loops and it will run faster. That is what the filter() and search() functions are for. The fact that you are using search() to jump through the loop is a big help, but you might consider adding some debug info, ? statements, so you can see how often the loop is iterating, and measure chunks of code for time to see where the slow spots are.

Also, it may be from posting to the forum, but watch your indentation.

Link to comment
Share on other sites

Thanks for Your reply...

I found mistake... the "delay(0.01)" near the end of script caused to work this so slow...

I did not taught that this can slow searching so much...

Now it founds the material and paremeters for less than two seconds...

Best regards

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.