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
kdoske
  • Members
  • 138 posts
  • Last active: Nov 06 2012 01:58 AM
  • Joined: 17 Dec 2008

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 ;-)


DONE! Changes have been posted on the first post. I havn't used AHK in 2 years. I'm a little rusty myself!

Albireo
  • Members
  • 558 posts
  • Last active: Dec 13 2019 02:02 PM
  • Joined: 01 Feb 2006
Welcome back and Thank You!
//Jan

  • Guests
  • Last active:
  • Joined: --
@KDoske: could you have a look at the lib you pasted in the first post, there seem to be some stray new lines in there in the commented text at the top

;When you CSV_Load a blank file you must specify the column count before adding new rows or columns with a command
;;; new lines here ;;;
similar to: %CSV_Identifier%CSV_TotalCols := 'column count'.

which should be just one line
In your new CSV_Load there seems to be some stray debug? code here:
String = a,b,c`r`nd,e,f,,"g`r`n",h`r`nB,`n"C`nC",D
  e:= """" ; the encapsulation character (tipical ")
  RegExNeedle:= "\n(?=[^" e "]*" e "([^" e "]*" e "[^" e "]*" e ")*([^" e "]*)\z)"
  String := RegExReplace(String, RegExNeedle , "" )
  StringReplace,String, String,`r,@,All ;only for see msgbox


colli020
  • Members
  • 1 posts
  • Last active: Nov 27 2014 04:31 AM
  • Joined: 22 Feb 2013

i have a question concerning the best functions or mix of to use from this library to get a "lookup" style script. honestly, I think that the CSV_MatchCellColumn() do the trick, but would like input of those better versed in programming than i before i move forward. here is what i am trying to accomplish:

 

In my job I get several files of different types employees; each employee type has a specific employee code convention, between 3 and 6 characters. Each month i get a new excel spreadsheet with all the edits from New and attrition. What i want to do type only the code into a gui I've created that returns the employee first last name into a text field on the gui.

 

When I converted the Excel file  csv file, it left many empty fields ",,,,,,,,,,,,,,,,,,,,,,," which I'm not sure how I can get rid of them,other than to delete them manually, They are close to 1000 so that isn't an option i would like take  :)  But that aside, would the CSV_MatchCellColumn() function ONLY search in the employee code column (I believe it's the 9th column), that way the entire needn't be read? And if answer to question YES,how would i return the first and second columns?

 

If is the incorrect topic,feel free to move it; I only placed it here because question was so closely related the library

 

colli020



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
You could indeed try to use the CSV library, but my advise would be to simply use COM and read the Excel files directly, this will save you the trouble of converting them to CSV first. See general intro here https://ahknet.autoh...iceapplications There is also specific tutorial for Excel here http://www.autohotke...rial-for-excel/

Andy22
  • Members
  • 2 posts
  • Last active: Sep 19 2015 08:06 PM
  • Joined: 22 Jun 2013

I just noticed a bug or unwanted behaviour. If the last character in a CSV file is a extra newline "`n", the Library will only import the first column?

In my case, the input files are all generated this way and i cant change this behaviour, so i added a small fix to the Load call.

CSV_Load(FileName, CSV_Identifier="", Delimiter="`,")
{
  Local Row
  Local Col
  temp :=  %CSV_Identifier%CSVFile
  FileRead, temp, %FileName%
  ;// fix for last newline and not importing all columns
  StringRight,NewlineCheck,temp,1
  if (NewlineCheck == "`n") {
    StringTrimRight, temp, temp,1
  }

......


aaronbaird1
  • Members
  • 5 posts
  • Last active: Jun 29 2013 02:10 AM
  • Joined: 30 Oct 2011

 

I just noticed a bug or unwanted behaviour. If the last character in a CVS file is a extra newline "`n", the Library will only import the first column?

In my case, the input files are all generated this way and i cant change this behaviour, so i added a small fix to the Load call.

CSV_Load(FileName, CSV_Identifier="", Delimiter="`,")
{
  Local Row
  Local Col
  temp :=  %CSV_Identifier%CSVFile
  FileRead, temp, %FileName%
  ;// fix for last newline and not importing all columns
  StringRight,NewlineCheck,temp,1
  if (NewlineCheck == "`n") {
    StringTrimRight, temp, temp,1
  }

......

 

omg thank you!!!!

That was driving me crazy.



neXt
  • Members
  • 549 posts
  • Last active: May 20 2015 02:38 AM
  • Joined: 18 Mar 2007

If anyone is working with large csv files and need to search them, I wrote a binary algorithm column search to improve performance. It returns the row number of found instance, or 0 if nothing was found.

 

The column must be sorted in ascending order, otherwise it won't work.

CSV_SearchColumnBinary(CSV_Identifier, SearchText, ColNumber)
{
  TotalRows := %CSV_Identifier%CSV_TotalRows
  Hi := %CSV_Identifier%CSV_TotalRows
  Lo := 0
 
  Loop
  {
    Mid := Floor((Hi - Lo) / 2)  + Lo
    if (Mid == oldMid)
    {
      break
    }
    Content := CSV_ReadCell(CSV_Identifier, Mid, ColNumber)
    ;MsgBox Hi %Hi% Lo %Lo% Mid %Mid%`ncurrent value: %Content%
    if (SearchText == Content)
    {
      return % Mid
    }
    else if (SearchText > Content)
    {
      Lo := Mid
    }
    else if (SearchText < Content)
    {
      Hi := Mid
    }
    oldMid := Mid
  }
  return 0
}