首页 > 解决方案 > 如何访问特定列是 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)
    }
}

标签: javacsvgroovy

解决方案


推荐阅读