AutoHotkey Community

It is currently May 27th, 2012, 6:56 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: January 4th, 2008, 5:47 pm 
Offline

Joined: April 17th, 2007, 1:37 pm
Posts: 761
Location: Florida
Credits: CSV rules pulled from Creativyst Software

Usage: Converts a string for use in a single field of a CSV file.
Will handle strings with: commas, double quotes, linefeeds*
Issues:Multi-line fields do not display properly in Excel (box character at each end of line).
*While creating a CSV with fields that contain linefeeds will work, there may not be an easy way (using AHK) to read a CSV file with this structure. I do not use mutli-line values but wanted to include this ability.


v Beta 1 - 20080414:
Code:
TheString=
(Join|
Smith, John
Auto"Hot"Key
Hogan, "Hulk"
This "double" quote
Plain old string
$1,000,000
Multi`nLine`nField
)
Loop, Parse, TheString, |
{
   FormattedString:=Format4CSV(A_LoopField)
   Results.=A_LoopField . " -> " . FormattedString . "`n"
}
MsgBox,,Results,%Results%
Return

Format4CSV(F4C_String)
{
   Reformat:=False ;Assume String is OK
   IfInString, F4C_String,`n ;Check for linefeeds
      Reformat:=True ;String must be bracketed by double quotes
   IfInString, F4C_String,`r ;Check for linefeeds
      Reformat:=True
   IfInString, F4C_String,`, ;Check for commas
      Reformat:=True
   IfInString, F4C_String, `" ;Check for double quotes
   {   Reformat:=True
      StringReplace, F4C_String, F4C_String, `",`"`", All ;The original double quotes need to be double double quotes
   }
   If (Reformat)
      F4C_String=`"%F4C_String%`" ;If needed, bracket the string in double quotes
   Return, F4C_String
}

v Alpha 1 - 20080104:
Code:
Go:
Reformat:=False  ;Assume we don't need to reformat
InputBox, SearchTerm, CSV Field Formatter v alpha 1, It places the text into the field...
If Errorlevel
   Reload ;For easy, live updating of script
SearchTerm_append:=SearchTerm
IfInString, SearchTerm_append,`, ;Check for commas
   Reformat:=True ;String must be bracketed by double quotes
IfInString, SearchTerm_append, `" ;Check for double quotes
{   Reformat:=True ;String must be bracketed by double quotes
   StringReplace, SearchTerm_append, SearchTerm_append, `",`"`", All ;And the original double quotes need to be double double quotes
}
If Reformat
   SearchTerm_append=`"%SearchTerm_append%`" ;If needed, bracket the string in double quotes
MsgBox, %SearchTerm%`nWas reformatted to`n%SearchTerm_append%
GoSub, Go

_________________
[Join IRC!]
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 14th, 2008, 3:41 pm 
Offline

Joined: April 17th, 2007, 1:37 pm
Posts: 761
Location: Florida
Bump for beta 1.

_________________
[Join IRC!]
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2011, 9:47 pm 
Offline

Joined: March 16th, 2011, 6:12 pm
Posts: 172
Location: Worcester, Massachusetts
Hey! I love CSV and found this while looking for AutoHotkey stuff related to CSV :)

Not to, uhhhh.... be that guy.... but this can be done a bit shorter. (I know this is 3½ years old so you probably already know/don't care.) Anyway, here's my function for doing this:
Code:
CSV(Text)
{
   If (SubStr(Text, 1, 1) = """") or InStr(Text, ",") or InStr(Text, "`n") or InStr(Text, "`r") {
      StringReplace, Text, Text, ", "", All
      Text := """" Text """"
   }
   Return Text
}

I think it's essentially the same as yours. Although there is one difference that I can see: see how I do (SubStr(Text, 1, 1) = """") instead of just InStr(Text, """") ? That's because, from my testing, I've found that only if the first character is a double quote is the value considered literal. For instance, cell 2 below contains a literal ":
Code:
Cell 1, "Cell 2


And another note (one which I am disappointed in!): It seems that AutoHotkey does not support newlines as value separators! (By "doesn't support" I mean not supported by Loop, Parse, String, CSV) For instance, if you create a test.csv file like the one shown below:
Code:
ab,cd
ef,gh
ij,kl
...and then run this code:
Code:
FileRead, string, test.csv
Loop, Parse, string, CSV
   MsgBox, %A_LoopField%
...it will display "ab" then "cd`nef" then "gh`nij" then "kl". No good!

I'm working on a solution to this but it's not quite done

_________________
★★★ Email me at berban at aim full stop com ★★★


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2011, 2:45 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
In CSV, non-literal newlines delimit rows, not cells, that's why Loop, Parse, CSV doesn't do anything special with newlines.

If you haven't already seen it, my table manipulation library has functions to convert CSV to/from TSV.

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2011, 4:50 am 
Offline

Joined: March 16th, 2011, 6:12 pm
Posts: 172
Location: Worcester, Massachusetts
Ah, thanks for the tip :)

Your lib looks great! An incredible variety of functions! :shock:

What exactly are you recommending I do: just use the tsv format instead; or read csv files and convert them to tsv and use your functions on them; or read a tsv and convert it to csv for better compatibility with Loop, Parse, CSV...?

Oh and by the way, personally, I like to put the url of the forum post in my code downloads, since that's where a lot of documentation is... which I don't see in your table lib. Just a thought

_________________
★★★ Email me at berban at aim full stop com ★★★


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2011, 11:13 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
berban_ wrote:
Your lib looks great! An incredible variety of functions! :shock:
Glad to hear it! I guess I should underscore that the functions are standalone, which means you can cherry-pick the ones you need instead of including the whole 100K+ lib.

I don't know what task you wish to accomplish, only that it probably involves tabular data. So, I will only recommend that you review available resources for handling such data and determine which, if any, meet your requirements. I think it's very reasonable to store data tables in CSV files and have a script convert them to TSV for processing.

I admit I'm a slouch in the documentation department, but I have added the url to the forum topic to the library file :wink:

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Yahoo [Bot] and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group