 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Joshua Guest
|
Posted: Thu Mar 08, 2007 12:11 pm Post subject: Splitting an excel file into an array, other than .csv |
|
|
Can anyone help me with this,
I'm using .csv array to read an excel file but one of the columns contains text with multiple commas. I do not want to seperate based on these commas, is there a way to seperate based on excel columns only?
Example:
Excel column A: 1235
Excel column B: last name, first name, DOB
Excel column C: class
I need to have each column read into it's own variable, including all of column B.
Thanks in advance,
Joshua |
|
| Back to top |
|
 |
Helpy Guest
|
Posted: Thu Mar 08, 2007 1:18 pm Post subject: |
|
|
Not too sure of what you mean, but in CSV, if a field has special chars like double quotes, commas or newlines, you have to put double quotes around it (and double the double quotes inside).
HTH. |
|
| Back to top |
|
 |
Dippy46
Joined: 06 Jul 2004 Posts: 171 Location: Manchester, England.
|
Posted: Thu Mar 08, 2007 2:00 pm Post subject: |
|
|
@ Joshua,
Well, if you look at the csv produced by Excel, as Helpy rightly says, you'll find fields containg commas are enclosed in quotes.
This can of course be used to our advantage. So while no-one has yet popped out of the woodwork with a ReGex solution, we'll use old fashioned methods
Sample csv generated by excel:
123,"one,two",5897,"john,doe",999,
5235,"four,five,six",2567,"fred,the,bear",277,183
32,"one,two,three,four","ted,smith",56,,
| Code: |
Loop, Read,YourExcelData.Csv
{
Loop,Parse,A_LoopReadLine,% chr(34)
{
StringReplace,Var1,A_LoopField,% Chr(44),% Chr(32),1
L .= Var1 ","
}
Z .= L "`n"
L := ""
}
FileAppend,% z ,Converted.Csv
MsgBox File Converted !
|
output file:
123 ,one two, 5897 ,john doe, 999 ,
5235 ,four five six, 2567 ,fred the bear, 277 ,183,
32 ,one two three four, ,ted smith, 56 ,
Have Fun. _________________ Simple ideas lie within reach, only of complex minds |
|
| Back to top |
|
 |
FeiVictas
Joined: 16 Nov 2006 Posts: 7
|
Posted: Thu Mar 08, 2007 6:46 pm Post subject: |
|
|
This is actually quite simple. AHK has the capability to read .csv exactly as Excel writes them (including support for literal quotes, commas, etc). Simply specify CSV as the delimiter in a parsing loop.
| Code: | Loop, Read, mycsv.csv
{
Loop, Parse, A_LoopReadLine, CSV
{
col%A_index% = %A_LoopField%
line := line . col%A_Index% . " - "
}
msgbox %line%
line =
}
|
|
|
| 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
|