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
SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

How do I adjust the code below to replace the data in File A column 2 with the data in File B Column 2?

Commented the code above, I don't understand your question as the script above does exactly that. It replaces the data in the 2nd column (B) in file A with the data from file b.

Edit: and you COULD use TF, see <!-- m -->http://www.autohotke... ... 868#366868<!-- m -->

MacroMan!
  • Members
  • 604 posts
  • Last active: Mar 20 2012 11:40 AM
  • Joined: 28 Aug 2009

Can you explain this line of code? StringReplace, ColB, ColB, `r`n, |, All


Replace all instances of | with `r`n (linebreak)
What ever happened, happened.

badmojo
  • Members
  • 204 posts
  • Last active: Jul 23 2014 01:39 AM
  • Joined: 11 Nov 2005
how can i load a column into a DDL control if the one of the item in that column has a comma as part of it?

GetColumn:=CSV_ReadCol(CSV_Identifier, 2)
...
Gui, +Delimiter`,
Gui, Add, DropDownList, AltSubmit vDDL1 gDDL1, %GetColumn%
Gui, Show


SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Bug report
CSV_MatchCellRow has a bug in it, see post and fix here
<!-- m -->http://www.autohotke... ... 667#400667<!-- m -->

- Look up the CSV_MatchCellRow function
- in that function there is a line

IfEqual, [color=red]CurrentRow[/color], %SearchText%
- change it to
IfEqual, [color=red]CurrentString[/color], %SearchText%



SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
@kdoske:

Suggestion for new function
see "Convert CSV into HTML table"
<!-- m -->http://www.autohotke...ic.php?p=401213<!-- m -->
for the idea (don't use the posted code there of course)

Paker
  • Guests
  • Last active:
  • Joined: --
Can anyone tell me if this lib has the ability to read a semicolon delimited text file and rearrange out-of-order rows?

The file should look like this:
A;B;C;D;E;F;G

Some rows look like this:
A;F;G;E;C;B;D

Is it possible to identify those rows and put them in the expected order?

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
You would read / load your file like so
CSV_Load("file.txt", Data, ";")
now each "cell" (row,column) is stored in memory which you can manipulate. Using a loop you can create a new CSV file (text file) making sure each cell is in the correct place

How do you know which lines are "bad"?

Pseudocode:

- Loop, NumberOfRows (see CSV Lib)
- If ROW = OK
then FILEAPPEND CELLS FROM ROW AS IS
ELSE
FILEAPPEND CELLS IN CORRECT ORDER
- done
Not saying it is the most efficient method, but if you can identify the BAD lines it isn't that complicated.

Edit: seems solved here <!-- m -->http://www.autohotke...topic67817.html<!-- m -->

segalion
  • Guests
  • Last active:
  • Joined: --
I was working with an old csv lib version and found some problems with some functions...
Could be good make some verifications like this ...
CSV_ReadRow(RowNumber)
  {
   Local RowData:=""
   if ( RowNumber<1 or RowNumber>CSV_TotalRows) {
	ErrorLevel=1
        return ""
        }
   Loop, %CSV_TotalCols%
     {
       RowData .= CSV_Row%RowNumber%_Col%A_Index%
      If A_Index <> %CSV_TotalCols%
         RowData .= CSV_Delimiter
     }
     ErrorLevel=0
     Return %RowData%
  }


Duplicity
  • Members
  • 10 posts
  • Last active: Nov 21 2011 09:50 PM
  • Joined: 18 Nov 2010
Just taking some time out to say thanks for this library. As a noob, it's been super useful and I have already used it a number of times. Great job!

  • Guests
  • Last active:
  • Joined: --

Just taking some time out to say thanks for this library. As a noob, it's been super useful and I have already used it a number of times. Great job!


Same here, many thanks! It seems to find itself of good use in a number of my scripts as well.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Bug report
Test script to replicate the problem. If someone could confirm that would be helpful. If you are reading multiple rows or columns in a row using CSV_ReadRow and/or CSV_ReadCol it "appends" your previous column or row to what is returned from the function, so you send up with multiple rows which is not what you expect.

Bugfixes for those who have this lib below the test code, add two lines see comments

f=testing.csv
csv= ; create test data file
(
1,2,3
4,5,6
7,8,9
0,a,b
c,d,e
)
FileDelete, %f%
FileAppend, %csv%, %f%
CSV_Load("testing.csv", "data") ; read testing.csv

Loop, % CSV_TotalRows("data")
	MsgBox % CSV_ReadRow("data", A_Index)

Loop, % CSV_TotalCols("data")
	MsgBox % CSV_ReadCol("data", A_Index)
	
/* Bug fixes

CSV_ReadRow(CSV_Identifier, RowNumber)
{
  Local CellData
  CurrentCSV_TotalCols := %CSV_Identifier%CSV_TotalCols 
	RowData= ; add this line
  Loop, %CurrentCSV_TotalCols%
  {
    RowData .= %CSV_Identifier%CSV_Row%RowNumber%_Col%A_Index%
    If A_Index <> %CurrentCSV_TotalCols%
      RowData .= "`,"
  }
  Return %RowData%
}

CSV_ReadCol(CSV_Identifier, ColNumber)
{
  Local CellData
  CurrentCSV_TotalRows := %CSV_Identifier%CSV_TotalRows   
  ColData= ; add this line
  Loop, %CurrentCSV_TotalRows%
  {
    ColData .= %CSV_Identifier%CSV_Row%A_Index%_Col%ColNumber%
    If A_Index <> %CurrentCSV_TotalRows%
      ColData .= "`,"
  }
  Return %ColData%
}

Edit

Bug in CSV_LVSave:
Format4CSV isn't used while exporting Listview data, quick fix:

In CSV_LVSave change line
FullRow .= CellData
to
FullRow .= Format4CSV(CellData)

Further improvement would be to skip empty rows although should probably be a parameter.

QuantumSingular
  • Members
  • 13 posts
  • Last active: Mar 13 2014 06:23 PM
  • Joined: 22 Nov 2010
Confirmed, I experienced this problem too but forgot to suggest this same bugfix. I believe there is also another bug in a function that uses a comma delimiter instead of the user's specified delimiter. I'll see if I can find this one again.

*Edit: There wasn't an actual bug, just a small annoyance. When I use CSV_ReadRow I expected it to feed me the same delimiter I specified upon loading the file instead of a comma.

I made the delimeter global by adding this (green text) to CSV_Load:
CSV_Load(FileName, CSV_Identifier="", Delimiter="`,")
{
[color=green]  Global
  _Delimiter := Delimiter[/color]
...

then I changed "`," to _Delimiter for the functions CSV_ReadRow and CSV_ReadCol.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
See my previous bug post above, found another one in CSV_LVSave.

(I'm finding these bugs because I'm developing a CSV quick filter/search app that uses the Listview functions form this lib for import/export, will post it as a separate script when completed)

Edit:
See CSV Quick Filter - CSVQF
<!-- m -->http://www.autohotke...pic.php?t=73344<!-- m -->

rhyssedwards
  • Guests
  • Last active:
  • Joined: --
For the creators of this script.

I had trouble with CSV_Save dropping the encapsulation from the CSV File. Thus it would then en up with only 1 column as returnDSVarray would only load 1 column next time without quotes

I Ammended CSV save to the following
Loop, %CurrentCSV_TotalCols%
   {
      Col := A_Index
	  EntireFile .= """"   ;added this line
      EntireFile .= Format4CSV(%CSV_Identifier%CSV_Row%Row%_Col%Col%)
	  EntireFile .= """"   ;added this line
      If (Col <> %CSV_Identifier%CSV_TotalCols)
         EntireFile .= %CSV_Identifier%CSV_Delimiter
   }

I am not sure why my setup is the first to have this problem as this is an old thread.

comedoo
  • Guests
  • Last active:
  • Joined: --
When I try :
teste.csv before script
1111,1111

CSV_Load("teste.csv",AA,";")
CSV_ModifyCell(AA,7777,1,1)
CSV_ModifyCell(AA,7777,1,2)
CSV_Save("teste.csv",AA,1)

teste.csv output
7777

so ever time it is delete the last row, also if I want change just first row the last will be deleted !!!
someone can help me ?
thx