 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Rhys
Joined: 17 Apr 2007 Posts: 761 Location: Florida
|
Posted: Fri Jan 04, 2008 4:47 pm Post subject: [Function] Convert String for CSV - Format4CSV() beta 1 |
|
|
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!]
 |
|
| Back to top |
|
 |
Rhys
Joined: 17 Apr 2007 Posts: 761 Location: Florida
|
Posted: Mon Apr 14, 2008 2:41 pm Post subject: |
|
|
Bump for beta 1. _________________ [Join IRC!]
 |
|
| Back to top |
|
 |
berban_
Joined: 16 Mar 2011 Posts: 150 Location: Worcester, Massachusetts
|
Posted: Fri Sep 30, 2011 8:47 pm Post subject: |
|
|
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 ":
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:...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 ★★★ |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
|
| Back to top |
|
 |
berban_
Joined: 16 Mar 2011 Posts: 150 Location: Worcester, Massachusetts
|
Posted: Sat Oct 01, 2011 3:50 am Post subject: |
|
|
Ah, thanks for the tip
Your lib looks great! An incredible variety of functions!
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 ★★★ |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Sat Oct 01, 2011 10:13 am Post subject: |
|
|
| berban_ wrote: | Your lib looks great! An incredible variety of functions! | 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  _________________ Ternary (a ? b : c) guide TSV Table Manipulation Library
Post code inside [code][/code] tags! |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|