我正在使用这篇文章将 CSV 文件转换为数组。一切都很好。但是我得到了一个文件,其中包含字段值中的额外引号,例如:
“bash:”快捷方式“是”
和
< code>"bash: \ "快捷键\ "是" 。
所以我尝试像这样替换这些引号:
<cffile action="read" file="#filePath#" variable="csvContent">
<cfset csvContent = reReplace(csvContent, '(?:[^,\r\n])"(?:[^,\r\n])', '"', 'ALL')>
<--- Then do the conversion --->
<cfset array = csvToArray(csv = csvContent)>
但是非捕获组不工作。我做错了什么?
还有别的办法吗?
编辑1:
我还尝试使用cfhttp
并得到以下错误:
<cfhttp name="csvToQuery" method="get" url="#url#" />
详细信息:验证列属性和目标文件中指定的列数
消息:行中的列数不正确。
stack trace:cold fusion . tagext . net . http tag $ InvalidColumnsException:行中的列数不正确。在coldfusion.tagext . net . httptag . conn helper(http tag . Java:1149)在cold fusion . tagext . net . http tag . doendtag(http tag . Java:1219)在cfmfhttp2 ecfm 308364137 . run page(C:\ inetpub \ wwwroot \ mfhttp . cfm:1)在cold fusion . runtime . cfjsppage . invoke(cfjsppage . Java:244)在cold fusion . tagextcfm servlet . service(cfm servlet . Java:219)at cold fusion . bootstrap . bootstrap servlet . service(bootstrap servlet . Java:89)at org . Apache . catalina . core . applicationfilterchain . internal dofilter(applicationfilter chain . Java:303)at org . Apache . catalina . core . applicationfilter chain . do filter(applicationfilter chain . Java:208)at cold fusion . monitor . event . monitoring servlet filter . do filter(monitoring servlet filter
哦,你不能这么容易地自己修复这种输入。正则表达式会进一步破坏你的数据。
你能在Java中创建一个小脚本来处理这个问题吗?如果你这样做了,然后使用uniVardy-parser来读取你的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示例:
>
将罐子装入Application.cfc
this.javaSettings = { loadPaths: ["C:\path\to\univocity-parsers-1.5.6.jar" ]};
使用 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);
}
}