使用Groovy在连续字段之后显示在CSV上的引号 [英] Quotes appearing on CSV after concatnate fields using Groovy
本文介绍了使用Groovy在连续字段之后显示在CSV上的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用groovy连接CSV中的两个字段
除了连接的字段显示引号外,它的工作正常。
I'm using groovy to concatenate two fields in CSV It's working ok except that the concatenated field is appearing with quotes.
以解决此问题?
ant.mkdir(dir:"target")
new File("target/UpsertCheckDeals.csv").withWriter {
new File("C:/Users/alon/Documents/CheckDealReadyForConcat.csv").splitEachLine(",") {Customer__c,Name__c,Deal__c,Check_Count__c ->
it.println "${Customer__c},${Deal__c},${Deal_Source__c},${Salesperson_Name__c},${Customer__c}-${Deal__c}"
推荐答案
CSV是一种比它更复杂的文件格式首次出现。
CSV is a more complicated file format than it would first appear. Fields can be optionally quoted which is what appears to be your problem.
大多数编程语言都有一个库,可以解析CSV文件。在groovy的情况下,我推荐opencsv
Most programming languages have a library that will parse CSV. In the case of groovy I'd recommend opencsv
http: //opencsv.sourceforge.net/
以下示例扩展了我为您的上一个问题。
The following example extends the example I created for your previous question.
├── build.xml
├── src
│ └── file1.csv
└── target
└── file1.csv
src / file1.csv
src/file1.csv
"customer",deal
"200000042",23
"200000042",34
"200000042",35
"200000042",65
/file1.csv
target/file1.csv
customer,deal,customer-deal
200000042,23,200000042-23
200000042,34,200000042-34
200000042,35,200000042-35
200000042,65,200000042-65
build.xml
build.xml
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">
<available classname="org.apache.ivy.Main" property="ivy.installed"/>
<target name="build" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
import com.opencsv.CSVReader
ant.mkdir(dir:"target")
new File("target/file1.csv").withWriter { writer ->
new File("src/file1.csv").withReader { reader ->
CSVReader csv = new CSVReader(reader);
csv.iterator().each { row ->
if (row.size() == 2) {
writer.println "${row[0]},${row[1]},${row[0]}-${row[1]}"
}
}
}
}
</groovy>
</target>
<target name="resolve" depends="install-ivy">
<ivy:cachepath pathid="build.path">
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
<dependency org="com.opencsv" name="opencsv" rev="3.8" conf="default"/>
</ivy:cachepath>
</target>
<target name="install-ivy" unless="ivy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
<fail message="Ivy has been installed. Run the build again"/>
</target>
</project>
注意:
- 使用 apache常春藤管理依附关系,例如groovy和opencsv
- 我包含了一个测试row.size()== 2,以防止空白行引发错误
- Uses apache ivy to managed dependencies like groovy and opencsv
- I included a test "row.size() == 2" to prevent empty rows throwing errors
这篇关于使用Groovy在连续字段之后显示在CSV上的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文