如何在 ColdFusion 中替换逗号分隔文件中不需要的逗号

How do I replace unwanted commas in a comma delimited file in ColdFusion

我正在加载 CSV,并尝试将内容插入 MySQL。其中一个字段中有逗号,例如:

 "Jane Doe","Boston","None","Yes","No"
 "John Doe","New York","No garlic, onions, or nuts","Yes","No"
 "Mary Doe","New York","None","Yes","No"

我从阅读文件开始:

<cffile action="read"file="/var/www/html/temp.csv" variable="datacsv"> 

然后我开始循环:

<cfloop index="index" list="#datacsv#" delimiters="#chr(13)#,#chr(10)#">
    <cfset item1 = Replace(listgetAt(index,1), """", "", "ALL")> #item1#<br>
    <cfset item2 = Replace(listgetAt(index,2), """", "", "ALL")> #item2#<br>
    <cfset item3 = Replace(listgetAt(index,3), """", "", "ALL")> #item3#<br>
    <cfset item4 = Replace(listgetAt(index,4), """", "", "ALL")> #item4#<br>
    <cfset item5 = Replace(listgetAt(index,5), """", "", "ALL")> #item5#<br>
</cfloop>

我的问题是,在第二项 (John Doe) 中,第三个字段中的那些逗号被解析为新字段。所以我要么需要弄清楚我遗漏了什么导致这种情况,要么去掉任何字段中的逗号并用不同的字符替换它们。

尝试使用此正则表达式将嵌入的逗号替换为破折号:

<cfscript>

    // CSV content
    csvContent = '"John Doe","New York","No garlic, onions, or nuts","Yes","No"';

    // Replace all comma in between with dash
    writeOutput(
        csvContent.replaceAll(
            ',(?!")|(?<!"),(?=[^"]*?"(?:,|\r?\n|\r|$))', '-'
        )
    );
</cfscript>

这里是 GIST.

编辑:

This works, but it is also stripping out any CR/LF in there. As I loop through my items, I am doing: <cfloop index="index" list="#csvContent#" delimiters="#chr(13)#,#chr(10)#">

您可以简单地使用 CR/LF( chr(13)chr(10) ) 作为分隔符。 这是一个例子:

<!--- CSV content --->
<cfset csvContent = '"John Doe","New York","No garlic, onions, or nuts","Yes","No"'>

<!--- Replace all comma in between with dash --->
<cfset newCsvContent = csvContent.replaceAll(
    ',(?!")|(?<!"),(?=[^"]*?"(?:,|\r?\n|\r|$))', '-'
)>

<!--- Process records --->
<cfoutput>
    <cfloop list="#newCsvContent#" index="csvRow" delimiters="#chr(13)##chr(10)#">
        Column 1: #replace( csvRow.listGetAt( 1 ), """", "", "ALL")#<br>
        Column 2: #replace( csvRow.listGetAt( 2 ), """", "", "ALL")#<br>
        Column 3: #replace( csvRow.listGetAt( 3 ), """", "", "ALL")#<br>
        Column 4: #replace( csvRow.listGetAt( 4 ), """", "", "ALL")#<br>
        Column 5: #replace( csvRow.listGetAt( 5 ), """", "", "ALL")#<br>
        <br>
    </cfloop>
</cfoutput>

使用 <cfhttp> 而不是 <cffile> 来读取文件。 name 属性为您提供了一个 query 属性。 This document 声明 textQualifier 的默认值是双引号,但无论如何我都会指定它。