4

我正在使用这篇文章将 CSV 文件转换为数组。一切正常。但是我得到了一个文件,其中包含字段值中的额外引号,例如:

"bash: "shortcuts" are"

"bash: \"shortcuts\" are".

所以我尝试像这样替换这些引号:

<cffile action="read" file="#filePath#" variable="csvContent">
<cfset csvContent = reReplace(csvContent, '(?:[^,\r\n])"(?:[^,\r\n])', '&quot;', 'ALL')>

<--- Then do the conversion --->
<cfset array = csvToArray(csv = csvContent)>

但是非捕获组不起作用。我究竟做错了什么?

有没有其他方法可以做到这一点?

编辑1:

我也尝试使用cfhttp并得到以下错误:

<cfhttp name="csvToQuery" method="get" url="#url#" />

详细信息:验证列属性和目标文件中指定的列数

消息:行中的列数不正确。

堆栈跟踪:coldfusion.tagext.net.HttpTag$InvalidColumnsException:行中的列数不正确。在coldfusion.tagext.net.HttpTag.connHelper(HttpTag.java:1149) 在coldfusion.tagext.net.HttpTag.doEndTag(HttpTag.java:1219) 在cfmfhttp2ecfm308364137.runPage(C:\inetpub\wwwroot\mfhttp.cfm: 1) 在coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:244) 在coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:446) 在coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65) 在Coldfusion.filter.IpFilter.invoke(IpFilter.java:64) 在coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:430) 在coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48) 在coldfusion.filter.MonitoringFilter .invoke(MonitoringFilter.java:40) 在coldfusion.filter.PathFilter.invoke(PathFilter.java:112) 在coldfusion.filter.LicenseFilter。

4

1 回答 1

2

哦,您将无法如此轻松地自己修复这种输入。正则表达式会进一步破坏您的数据。

你能用 Java 创建一个小脚本来处理它吗?如果这样做,则使用uniVocity-parsers读取您的 CSV 输入并使用正确的引号转义将其写回:

这是唯一可以处理断引号转义的 CSV 解析器。试试这个例子:

import com.univocity.parsers.csv;

import java.io.*;
import java.util.*;

public class Test {

    public static void main(String ... args){
        CsvParserSettings settings = new CsvParserSettings();
        settings.getFormat().setLineSeparator("\r\n");
        settings.setParseUnescapedQuotes(true); // THIS IS IMPORTANT FOR YOU
        CsvParser parser = new CsvParser(settings);

        String line1 = "something,\"a quoted value \"with unescaped quotes\" can be parsed\", something\r\n";
        System.out.println("Input line: " + line1);

        String line2 = "\"after the newline \r\n you will find \" more stuff\r\n";
        System.out.println("Input line: " + line2);

        List<String[]> allInputLines = parser.parseAll(new StringReader(line1 + line2));

        System.out.println("===============\nParsed input values\n===============");
        int count = 0;
        for(String[] line : allInputLines){
            System.out.println("From line " + ++count + ":");
            for(String element : line){
                System.out.println("\t" + element);

            }
            System.out.println();
        }

        //Let's write your output CSV
        StringWriter output = new StringWriter();
        CsvWriterSettings writerSettings = new CsvWriterSettings();
        writerSettings.getFormat().setLineSeparator("\r\n");
        writerSettings.getFormat().setQuoteEscape('\\'); //it seems you are using backslash as quote escape
        writerSettings.getFormat().setCharToEscapeQuoteEscaping('\\'); //when your quote escape character is not the same as the quote character, you might need to escape the escape character as well
        writerSettings.setQuoteAllFields(true); //let's force quotes on all fields so whatever is parsing your input file has more  chance of doing it properly
        CsvWriter writer = new CsvWriter(output, writerSettings);

        for(String[] row : allInputLines){
            writer.writeRow(row);
        }
        writer.close();

        System.out.println("===============\nNicely formatted output\n===============");
        System.out.println(output.toString());

    }

}

此代码将产生以下输出(可能会被您的数据导入工具读取):

Input line: something,"a quoted value "with unescaped quotes" can be parsed", something

Input line: "after the newline 
you will find " more stuff

===============
Parsed input values
===============
From line 1:
    something
    a quoted value "with unescaped quotes" can be parsed
    something

From line 2:
    after the newline 
you will find " more stuff


===============
Nicely formatted output
===============
"something","a quoted value \"with unescaped quotes\" can be parsed","something"

"after the newline 
 you will find \" more stuff"

披露:我是这个库的作者。它是开源和免费的(Apache V2.0 许可证)。

ColdFusion 10+ 示例:

  1. 在 Application.cfc 中加载 jar

    this.javaSettings = { loadPaths: ["C:\path\to\univocity-parsers-1.5.6.jar" ]};
    
  2. 使用 createObject 创建解析器类的实例:

    filePath = "c:\path\to\yourFile.csv";
    settings = createObject("java", "com.univocity.parsers.csv.CsvParserSettings").init();
    settings.getFormat().setLineSeparator(chr(13)& chr(10));
    settings.getFormat().setQuoteEscape("\");
    settings.setParseUnescapedQuotes(true); // THIS IS IMPORTANT FOR YOU
    parser = createObject("java", "com.univocity.parsers.csv.CsvParser").init(settings);
    reader = createObject("java", "java.io.StringReader").init(fileRead(filePath));
    arrayOfLines = parser.parseAll(reader);
    
    // display results
    counter = 1;
    for (line in arrayOfLines) {
        writeOutput("<br>From line "& (counter++) & ":");
        for (element in line) {
           writeOutput("<br>"& element);
        }
    }
    
于 2015-06-09T07:03:29.090 回答