java - 如何访问特定列是 csv 文件?如果该列中的单元格为空,则根本不要在 csv 文件中写入行
问题描述
工作的简要总结。我有一个带有 xml 文件的 zip 文件夹。我解压缩它,将其发送到另一个目标文件夹。对于文件夹中的所有 xml 文件。我需要获取文件名、xpath、特定元素的值。我做了所有这些。
我需要做的是访问 csv 文件中的特定列(标签列),检查是否有任何单元格为空,如果它是空的,我根本不想在 csv 文件中写入整行。希望我足够清楚它是一个 groovy/java 工作
public void getAllFiles(String fileName) {
//create csv file in a variable and hold all files in a single variable as well
def xmlFile = new XmlSlurper().parse(new File(fileName))
def csvFile = new File("C:\\Users\\username\\Desktop\\test1.csv")
def labels = xmlFile.breadthFirst().findAll {
if (it.name() == 'element1') {
println it
println it.attributes().get("attribute")
println getXPath(it)
def langs = it.attributes().get("attribute")
def description = it.parent().parent().children().find { node -> node.name() == 'message' }.text().toString()
def xmlFile2 = new File(fileName)
csvFile.append('"' + xmlFile2.getName() + '"' + "," + '"' + getXPath(it) + '"' + "," + '"' + description + '"' + "," + '"' + langs + '"' + "," + '"' + it + '"' + "\r\n")
}
}
}
def list = []
def ant = new AntBuilder()
ant.unzip(src: "zipfolderpath.zip", dest: "path_to_folder",
overwrite: "false")
def dir = new File("Path_to_folder")
def csvFile = new File("C:\\Users\\username\\Desktop\\test1.csv")
def valueA = "XPATH"
def valueE = "FILENAME"
def valueD = "DESCRIPTION"
def valueB = "LANGUAGE"
def valueC = "LABELS"
csvFile.write("\r\n" + valueE + "," + valueA + "," + valueD + "," + valueB + "," + valueC + "\r\n")
dir.eachFileRecurse(FileType.FILES) { file ->
if (file.getName().endsWith("xml")) {
extractFileName(file.absolutePath)
}
}
解决方案
推荐阅读
- java - 我怎样才能使这段代码更简单?另外我如何检查输入是否为int?
- oracle - Oracle 根据 id 确定表空间名称
- java - 如何通过 intellij IDE 的 maven 插件添加新的 azure 功能
- google-apps-script - 如何使用 UrlFetchApp 为 BigQuery Rest API 添加媒体上传?
- amazon-web-services - 我正在将本地 Kibana 服务器(ec2 实例)连接到 Amazon ES
- java - Docker compose spring boot 无法连接到 mysql db
- debugging - STM32 EVAL 板电源的奇怪行为
- docker - 容器运行时未运行:输出:无法连接到位于 unix:///var/run/docker.sock 的 Docker 守护程序。docker 守护进程是否正在运行?
- vb.net - 如果它们在 01 到 15 的第二列中有记录,如何遍历 datagridview 行以删除条目?
- c# - C#拦截文件读取