csv 半手动数据统计
背景
每日数据统计
全网汇总数据

慕课手记

简书

博客园

腾讯云社区

懒则想法偷懒
回顾操作流程
思考问题瓶颈
梳理操作流程
手动复制文章



程序分析提取




小结
最后更新于












最后更新于
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.6</version>
</dependency>/**
* 写入csv文件
*
* @param data 数据内容
* @param filePath 文件路径
* @throws IOException
**/
public static void writeCsv(List<String[]> data, String filePath) throws IOException {
FileWriter fw = new FileWriter(new File(filePath));
final CSVPrinter printer = CSVFormat.EXCEL.print(fw);
printer.printRecords(data);
printer.flush();
printer.close();
}
/**
* 读取csv文件
*
* @param filePath 文件路径
* @return CSVRecord 迭代对象
* @throws IOException
**/
public static Iterable<CSVRecord> readCSV(String filePath) throws IOException {
InputStream inputStream = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(inputStream);
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(isr);
return records;
}
/**
* 测试写入并读取csv 文件
*/
private static void testWriteAndRead() throws IOException {
//写入数据
List<String[]> data = new ArrayList<String[]>();
data.add(new String[]{"张三", "18", "3000"});
data.add(new String[]{"李四", "20", "4000"});
data.add(new String[]{"王二", "25", "5000"});
//写入文件路径
String path = "/Users/sunpo/Downloads/testWriteAndRead.csv";
//写入 csv 文件
writeCsv(data, path);
//读取文件
Iterable<CSVRecord> records = readCSV(path);
for (CSVRecord record : records) {
for (String string : record) {
System.out.print(string);
System.out.print(" ");
}
System.out.println();
}
}148浏览 2推荐 0评论
204浏览 2推荐 0评论
181浏览 2推荐 0评论List<String> row = StringTools.splitToListString(string, " ");String readCountWithDescString = row.get(0);
String readCountString = StringUtils.substringBefore(readCountWithDescString, "浏览");
String recommendCountWithDescString = row.get(1);
String recommendCountString = StringUtils.substringBefore(recommendCountWithDescString, "推荐");
String commentCountWithDescString = row.get(2);
String commentCountString = StringUtils.substringBefore(commentCountWithDescString, "评论");//浏览数
int readCount = 0;
//推荐数
int recommendCount = 0;
//评论数
int commentCount = 0;
readCount += Integer.parseInt(readCountString);
recommendCount += Integer.parseInt(recommendCountString);
commentCount += Integer.parseInt(commentCountString);/**
* 统计慕课手记
*
* @throws IOException
*/
private static void countImooc() throws IOException {
//昨日统计数据
String yesterday = DateFormatUtils.format(DateUtils.addDays(new Date(), -1), "yyyyMMdd");
String path = String.format("/Users/sunpo/Documents/workspace/count/imooc-%s.csv", yesterday);
//总行数
int allRows = 0;
//有效行数
int allValidRows = 0;
//当前行是否有效
boolean isValidRow = true;
//浏览数
int readCount = 0;
//推荐数
int recommendCount = 0;
//评论数
int commentCount = 0;
Iterable<CSVRecord> records = readCSV(path);
for (CSVRecord record : records) {
allRows++;
for (String string : record) {
System.out.println(string);
if (StringUtils.isBlank(string)) {
isValidRow = false;
break;
}
List<String> row = StringTools.splitToListString(string, " ");
String readCountWithDescString = row.get(0);
String readCountString = StringUtils.substringBefore(readCountWithDescString, "浏览");
String recommendCountWithDescString = row.get(1);
String recommendCountString = StringUtils.substringBefore(recommendCountWithDescString, "推荐");
String commentCountWithDescString = row.get(2);
String commentCountString = StringUtils.substringBefore(commentCountWithDescString, "评论");
readCount += Integer.parseInt(readCountString);
recommendCount += Integer.parseInt(recommendCountString);
commentCount += Integer.parseInt(commentCountString);
}
if (isValidRow) {
allValidRows++;
}
isValidRow = true;
}
System.out.println();
System.out.println(String.format("[慕课手记] 一共读取%d行,有效行: allValidRows = %d ,其中浏览数: readCount = %d ,推荐数: recommendCount = %d ,评论数: commentCount = %d", allRows, allValidRows, readCount, recommendCount, commentCount));
System.out.println();
}