Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

CSV Library [lib]


  • Please log in to reply
52 replies to this topic
comedoo
  • Guests
  • Last active:
  • Joined: --
ops teste.csv input is> 1111;1111
sorry

  • Guests
  • Last active:
  • Joined: --
Your errors marked in red
CSV_Load("teste.csv",[color=red]"[/color]AA[color=red]"[/color],[color=red]","[/color])
CSV_ModifyCell([color=red]"[/color]AA[color=red]"[/color],7777,1,1)
CSV_ModifyCell([color=red]"[/color]AA[color=red]"[/color],7777,1,2)
CSV_Save("teste.csv",[color=red]"[/color]AA[color=red]"[/color],1)


comedoo
  • Members
  • 2 posts
  • Last active: Apr 03 2012 12:42 PM
  • Joined: 09 Jan 2012
Thank u for answer so fast.
CSV_Load("teste.csv","AA",";")
CSV_ModifyCell("AA",2020,1,1)
CSV_Save("teste.csv","AA",1)
when it modify only one cell, the script delete the last row
csv in: 1010;1010
csv out: 2020

  • Guests
  • Last active:
  • Joined: --
I can not replicate your problem with this test script
f= ; create test file
(join`r`n
1111;1111
)
filedelete,f.csv
fileappend,%f%,teste.csv

CSV_Load("teste.csv is OK","AA",";")
CSV_ModifyCell("AA",2020,1,1)
MsgBox % CSV_ReadCell("AA",1,1) ":" CSV_ReadCell("AA",1,2)
CSV_Save("teste.csv","AA",1)

2020;1111



  • Guests
  • Last active:
  • Joined: --
That should be
CSV_Load("teste.csv","AA",";")
of course

comedoo
  • Members
  • 2 posts
  • Last active: Apr 03 2012 12:42 PM
  • Joined: 09 Jan 2012
Ok, it's work, thank u

segalion
  • Members
  • 50 posts
  • Last active: Oct 16 2014 09:20 AM
  • Joined: 02 Feb 2011
Some regex sugestion to improve CSV load:

pre filter EOL inside encapsulations (in this change `r`n or `n inside encapsulation e to `r )
String = a,b,c`r`nd,e,f,,"g`r`n",h`r`nB,`n"C`nC",D
e:= """" ; the encapsulation character (tipical ")

; change `r`n or `n inside encapsulation e to `r 
RegExNeedle:= "\n(?=[^" e "]*" e "([^" e "]*" e "[^" e "]*" e ")*([^" e "]*)\z)"
String := RegExReplace(String, RegExNeedle , "" ) 

StringReplace,String, String,`r,@,All ;only for see msgbox
msgbox, : %String% :


  • Guests
  • Last active:
  • Joined: --
It's a shame kdoske doesn't seem to be around to include the fixes.
But perhaps someone will prepare a new AutoHotkey v2 compatible version when v2 is out of alpha ;-)

gpzs
  • Guests
  • Last active:
  • Joined: --
How can i get this to not break when the csv has a new line in it?
One of my cells contains this:
<!--URLs OF IMAGES USED IN THIS LISTING

photo1:
url1
...
But alls i get is this from the cell.
<!--URLs OF IMAGES USED IN THIS LISTING


  • Guests
  • Last active:
  • Joined: --
There seems to be a bug in CSV_Load
It wont detect the correct number of colums. first it loops the correct amount, then it loops and sets it back to 1, at least with my csv. i corrected it with this.

CSV_Load(FileName, CSV_Identifier="", Delimiter="`,")
{
  Local Row
  Local Col
  temp :=  %CSV_Identifier%CSVFile
  FileRead, temp, %FileName%
  StringReplace, temp, temp, `r`n`r`n, `r`n, all   ;Remove all blank lines from the CSV file
  loopnumber = 1
  Loop, Parse, temp, `n, `r
  {
    if loopnumber = 1
    Col := ReturnDSVArray(A_LoopField, CSV_Identifier . "CSV_Row" . A_Index . "_Col", Delimiter)
    loopnumber ++
    Row := A_Index
    Loop, Parse, A_LoopReadLine, %Delimiter%
    {
      Col := A_Index
      %CSV_Identifier%CSV_Row%Row%_Col%Col% := A_LoopField
    }
  }
  %CSV_Identifier%CSV_TotalRows := Row
  %CSV_Identifier%CSV_TotalCols := Col
  %CSV_Identifier%CSV_Delimiter := Delimiter
  SplitPath, FileName, %CSV_Identifier%CSV_FileName, %CSV_Identifier%CSV_Path

  IfNotInString, FileName, `\
  {
    %CSV_Identifier%CSV_FileName := FileName
    %CSV_Identifier%CSV_Path := A_ScriptDir
  }
  %CSV_Identifier%CSV_FileNamePath := %CSV_Identifier%CSV_Path . "\" . %CSV_Identifier%CSV_FileName
}


  • Guests
  • Last active:
  • Joined: --
You can (must really) remove the inner loop the ReturnDSVArray already does what the inner loop does so no need for the repetition.
Loop, Parse, temp, `n, `r
  {
    if loopnumber = 1
    Col := ReturnDSVArray(A_LoopField, CSV_Identifier . "CSV_Row" . A_Index . "_Col", Delimiter)
    loopnumber ++
    Row := A_Index
;    Loop, Parse, A_LoopReadLine, %Delimiter% ; don't need this, comment or remove
;    {
;      Col := A_Index
;      %CSV_Identifier%CSV_Row%Row%_Col%Col% := A_LoopField
    }
  }

CSV_Load / ReturnDSVArray also returns columns 1 if the very last field in the last row isn't closed properly, either if there is a missing " in case it needs one OR if is actually an empty line.

VVVVVVVVVV
  • Members
  • 2 posts
  • Last active: Nov 23 2010 04:46 AM
  • Joined: 03 Nov 2010
i trying to save the loaded csv file back in the same file that it was loaded from, but it only save the first column,when i call the number of columns it returns only 1 even though i can read from the other columns. i know its probably a noob mistake but i tryed and i can't solve it, my first time posting,sorry if i did something wrong

  • Guests
  • Last active:
  • Joined: --
Check the post above yours and read the last remark. Your last line in your CSV file is probably either an empty line or it is not closed of properly with an " if there actually should be one. Fix that in your CSV and it will work. Also note the couple of bug fixes posted a few pages back.

VVVVVVVVVV
  • Members
  • 2 posts
  • Last active: Nov 23 2010 04:46 AM
  • Joined: 03 Nov 2010
thx for the fast reply,i will check the others posts later, but for now i just wanted to know if theres some way to save exactly what its load in the CSV_load, because my .csv will have empty lines in some cases, why can't i save what im reading,i just need to modify some cells and save it again no fancy formating,do i need to put something in the end of the last data. like dog;cat";" i will check the other posts later but i need a quick solution for now,just don't have much time to finish the code, thx in advance.

  • Guests
  • Last active:
  • Joined: --
YOu can't save because it is a bug of the csv lib posted in the first post of this thread. So there is no quick solution. Kdoske left the forum (at least he hast returned in the last 18 months even though I pm-ed him). You can try the TSV library <!-- m -->https://ahknet.autoh...ot/faq.html#csv<!-- m --> which can handle CSV as well.