-1

我有一个 CSV 文件,其中包含从 Fitbit 下载的数据。CSV 文件中的数据遵循基本格式:

<Type of Data>
<Columns-comma-separated>
<Data-related-to-columns>

这是文件布局的一个小示例:

Activities
Date,Calories Burned,Steps,Distance,Floors,Minutes Sedentary,Minutes Lightly Active,Minutes Fairly Active,Minutes Very Active,Activity Calories
"2016-07-17","3,442","9,456","4.41","12","612","226","18","44","1,581"
"2016-07-18","2,199","7,136","3.33","10","370","93","12","46","1,092"
...other logs
Sleep
Date,Minutes Asleep,Minutes Awake,Number of Awakenings,Time in Bed
"2016-07-17","418","28","17","452"
"2016-07-18","389","26","10","419"

现在,我正在使用CSVParserApache Common 的库来查看这些数据。我的目标是将其转换为可以将相关数据转换为 Json 的 Java 对象(我需要将 Json 上传到不同的网站)。CSVParser有一个迭代器,我可以用它来遍历CSVRecords文件中的 。所以,基本上,我有一个所有数据的“列表”。
因为该文件包含不同类型的数据(睡眠日志、活动日志等),所以我需要获取该文件的一个小节/子列表,并将其传递给一个类进行分析。

我需要遍历列表并查找标识文件新部分的关键字(例如活动、食物、睡眠等)。一旦我确定了文件的下一部分是什么,我需要选择以下所有行,直到下一个类别。

现在,对于这个问题中的问题:我不知道如何使用迭代器来获得List.sublist(). 这是我一直在尝试的:

while (iterator.hasNext())
{
    CSVRecord current = iterator.next();
    if (current.get(0).equals("Activities"))
    {
        iterator.next(); //Columns
        while (iterator.hasNext() && iterator.next().get(0).isData()) //isData isn't real, but I can't figure out what I need to do.
        {
            //How do I sublist it here?
        }
    }
}

因此,我需要确定下一个是否CSVRecord以引用/有数据开头,并循环直到找到下一个类别,最后将文件的一个子部分(使用迭代器)传递给另一个函数以使用正确的日志执行某些操作。

编辑

我考虑先将其转换为List带有 while 循环的 a,然后再列出,但这似乎很浪费。如果我错了,请纠正我。

另外,我不能假设每个部分后面都有相同数量的行。它们可能有相似之处,但也有食物原木,它们遵循完全不同的模式。这是两个不同的日子。Foods遵循正常模式,但食物日志没有。

Foods
Date,Calories In
"2016-07-17","0"
"2016-07-18","1,101"

Food Log 20160717
Daily Totals
"","Calories","0"
"","Fat","0 g"
"","Fiber","0 g"
"","Carbs","0 g"
"","Sodium","0 mg"
"","Protein","0 g"
"","Water","0 fl oz"

Food Log 20160718
Meal,Food,Calories
"Lunch"
"","Raspberry Yogurt","190"
"","Almond Sweet & Salty Granola Bar","140"
"","Goldfish Baked Snack Crackers, Cheddar","140"
"","Bagels, Whole Wheat","190"
"","Braided Twists Honey Wheat Pretzels","343"
"","Apples, raw, gala, with skin - 1 medium","98"
"Daily Totals"
"","Calories","1,101"
"","Fat","21 g"
"","Fiber","13 g"
"","Carbs","202 g"
"","Sodium","1,538 mg"
"","Protein","28 g"
"","Water","24 fl oz"
4

1 回答 1

1

做你想做的最简单的方法是简单地记住以前的类别数据,当你点击一个新的类别时,处理以前的类别数据并为下一个类别重置。这应该有效:

String categoryName = null;
List<List<String>> categoryData = new ArrayList<>();
while (iterator.hasNext()) {
    CSVRecord current = iterator.next();
    if (current.size() == 1) { //start of next category
        processCategory(categoryName, categoryData);
        categoryName = current.get(0);
        categoryData.clear();
        iterator.next(); //skip header
    } else { //category data
        List<String> rowData = new ArrayList<>(current.size());
        CollectionUtils.addAll(rowData, current.iterator()); //uses Apache Commons Collections, but you can use whatever
        categoryData.add(rowData);
    }
}
processCategory(categoryName, categoryData); //last category of file

接着:

void processCategory(String categoryName, List<List<String>> categoryData) {
    if (categoryName != null) { //first category of the file, skip
        //do stuff
    }
}

以上假设 aList<List<String>>是您要处理的数据结构,但您可以根据需要进行调整。我什至可能会建议简单地传递List<Iterable<String>>给 process 方法(CSVRecordimplements Iterable<String>)并在那里处理行数据。

这绝对可以进一步清理,但它应该让你开始。

于 2016-07-18T20:57:00.997 回答