Solved

csv files and commas in data fields

Posted on 2008-10-22
6
693 Views
Last Modified: 2013-12-24
I have a csv file that needs to be imported to mysql, and then get locations of photos, and rework them. example looks like this:

"Product1","Red","Small","http://www.abc.com/image1.jpg,http://www.com/image2.jpg,http://www.com/image3.jpg","Available"
"Product2","Blue","Small","http://www.abc.com/image1.jpg,http://www.com/image2.jpg,http://www.com/image3.jpg","Available"
"Product3","Yellow","Small","http://www.abc.com/image1.jpg,http://www.com/image2.jpg,http://www.com/image3.jpg","Available"

I can successfully get the data out, but when i try to get the images, it treats them as separate columns. The code I'm using is like that below. How do I grab all the photo column as one column? All columns are separated by double quotes.
<cfloop file="D:\inetpub\seolot\test\csvdemo.csv" index="index" from="2" to="2"> 
 
<cfquery name="importcsv" datasource="seolot"> 
INSERT INTO csvdemo 
         (
         ProductName,
         Color,
         Size
         ) 
         
         VALUES
         (
         '#ReplaceNoCase(listgetAt('#index#',1, ','),'"','','ALL' )#', 
         '#ReplaceNoCase(listgetAt('#index#',2, ','),'"','','ALL' )#',  
         '#ReplaceNoCase(listgetAt('#index#',3),'"','','ALL' )#' 
         ) 
</cfquery> 
 
<!--- GET LIST OF PHOTOS --->
<cfset PhotoList = listgetAt('#index#',4)>
 
<!--- REMOVE DOUBLE QUOTES --->
<cfset PhotoList = #ReplaceNoCase(Photolist,'"','','ALL' )#>
 
1st photo: #listgetAt('#PhotoList#',1)#<br />
 
<br />
<cfloop list="PhotoList" index="i" delimiters=","> 
<cfset PhotoList = listgetAt('#PhotoList#',#i#, ',')>
 
Photo #i#: #PhotoList#<br />
</cfloop>
   
</cfloop>

Open in new window

0
Comment
Question by:monosyth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 100 total points
ID: 22779753
Check this:
<cfloop file="D:\inetpub\seolot\test\csvdemo.csv" index="index" > 
<cfquery name="importcsv" datasource="seolot"> 
  INSERT INTO csvdemo (ProductName, Color, Size ) 
     VALUES (
         '#ReplaceNoCase(listgetAt('#index#',1, ','),'"','','ALL' )#', 
         '#ReplaceNoCase(listgetAt('#index#',2, ','),'"','','ALL' )#',  
         '#ReplaceNoCase(listgetAt('#index#',3),'"','','ALL' )#' 
     ) 
</cfquery>
<!--- GET LIST OF PHOTOS --->
<cfset PhotoList = listgetAt('#index#',7,'"')>
<!--- NO NEED TO REMOVE DOUBLE QUOTES --->
<!--- cfset PhotoList = #ReplaceNoCase(Photolist,'"','','ALL' )# --->
<cfset i=0 >
<cfloop list="#PhotoList#" index="Photo" delimiters=","> 
 <cfset i=i+1 >
 Photo #i#: #Photo#<br />
</cfloop>
</cfloop>

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 22779771
Also be aware that you do not need no quote chars nor hash chars inside CF functions:

<cfset PhotoList = listgetAt(index,7,'"') >

0
 
LVL 8

Accepted Solution

by:
eszaq earned 400 total points
ID: 22781076
Try this, a little bit more compact:
<cfloop file="D:\inetpub\seolot\test\csvdemo.csv" index="index" from="2" to="2">
<!--- change list qualifiers to pipes "|" using regular expressions --->
 <cfset index = rereplace(i,'#chr(34)#(,#chr(34)#)?','|','all')>

<cfquery name="importcsv" datasource="seolot">
INSERT INTO csvdemo
         (
         ProductName, Color, Size, Images
         )
         
         VALUES
         (
         '#listgetAt(index,1, '|')#',
         '#listgetAt(index,2, '|')#',
         '#listgetAt(index,3, '|')#',
         '#listgetAt(index,4, '|')#',
         )
</cfquery>
0
Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

 
LVL 8

Expert Comment

by:eszaq
ID: 22781090
OOOPS, used wrong variable name in my code. Of course it should be:
 <cfset index = rereplace(index,'#chr(34)#(,#chr(34)#)?','|','all')>
0
 
LVL 8

Expert Comment

by:eszaq
ID: 22781106
And OF COURSE (dah) i have extra comma at the end of insert query.
0
 

Author Comment

by:monosyth
ID: 22781938
I ended up using a bit of both, and also I used this code below to figure out the null spaces.

<cfscript>
function listFix(list) {
var delim = '|';
var null = "NULL";
var special_char_list = "\,+,*,?,.,[,],^,$,(,),{,},|,-";
var esc_special_char_list = "\\,\+,\*,\?,\.,\[,\],\^,\$,\(,\),\{,\},\|,\-";
var i = "";
 
if(arrayLen(arguments) gt 1) delim = arguments[2];
if(arrayLen(arguments) gt 2) null = arguments[3];
 
if(findnocase(left(list, 1),delim)) list = null & list;
if(findnocase(right(list,1),delim)) list = list & null;
 
i = len(delim) - 1;
while(i GTE 1){
   delim = mid(delim,1,i) & "_Separator_" & mid(delim,i+1,len(delim) - (i));
   i = i - 1;
}
 
delim = ReplaceList(delim, special_char_list, esc_special_char_list);
delim = Replace(delim, "_Separator_", "|", "ALL");
 
list = rereplace(list, "(" & delim & ")(" & delim & ")", "\1" & null & "\2", "ALL");
list = rereplace(list, "(" & delim & ")(" & delim & ")", "\1" & null & "\2", "ALL");
    
return list;
}
</cfscript>
 
<!--- THEN PUT THIS INSIDE YOUR LOOP --->
 <cfset index = listFix(index)>

Open in new window

0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question