AutoHotkey Community

It is currently May 24th, 2012, 8:00 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: March 8th, 2007, 1:11 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 8th, 2007, 2:18 pm 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 8th, 2007, 3:00 pm 
Offline

Joined: July 6th, 2004, 10:07 am
Posts: 171
Location: Manchester, England.
@ 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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 8th, 2007, 7:46 pm 
Offline

Joined: November 16th, 2006, 10:53 pm
Posts: 7
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 =
}


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: billym, Google [Bot], patgenn123, Pulover, Xx7 and 72 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