我在执行获取csv文件维度的任务时的常规程序如下:
我使用while循环读取每一行并通过每次成功读取计数。缺点是读取整个文件需要时间来计算它有多少行。
String[] temp = lineOfText.split(",");
然后取大小。有更聪明的方法吗?喜欢:
file1 = read.csv;
xDimention = file1.xDimention;
yDimention = file1.yDimention;
答案 0 :(得分:3)
我想这取决于结构的规则程度,以及你是否需要一个确切的答案。
我可以想象查看前几行(或随机跳过文件),然后将文件大小除以平均行大小以确定粗略的行数。
如果您控制这些文件的编写方式,您可能会标记它们或在其旁边添加包含行数的元数据文件。
严格地说,你分割线的方式并没有涵盖所有可能的情况。 "hello, world", 4, 5
应该读为3列,而不是4列。
答案 1 :(得分:2)
您的方法无法使用多行值(您将获得无效的行数)和可能包含分隔符的引用值(您将获得无效的数字列)。
您应该使用CSV解析器,例如univocity-parsers提供的解析器。
使用uniVocity CSV解析器,确定尺寸的最快方法是使用以下代码。它解析 150MB文件,以 1.2秒显示其尺寸:
// Let's create our own RowProcessor to analyze the rows
static class CsvDimension extends AbstractRowProcessor {
int lastColumn = -1;
long rowCount = 0;
@Override
public void rowProcessed(String[] row, ParsingContext context) {
rowCount++;
if (lastColumn < row.length) {
lastColumn = row.length;
}
}
}
public static void main(String... args) throws FileNotFoundException {
// let's measure the time roughly
long start = System.currentTimeMillis();
//Creates an instance of our own custom RowProcessor, defined above.
CsvDimension myDimensionProcessor = new CsvDimension();
CsvParserSettings settings = new CsvParserSettings();
//This tells the parser that no row should have more than 2,000,000 columns
settings.setMaxColumns(2000000);
//Here you can select the column indexes you are interested in reading.
//The parser will return values for the columns you selected, in the order you defined
//By selecting no indexes here, no String objects will be created
settings.selectIndexes(/*nothing here*/);
//When you select indexes, the columns are reordered so they come in the order you defined.
//By disabling column reordering, you will get the original row, with nulls in the columns you didn't select
settings.setColumnReorderingEnabled(false);
//We instruct the parser to send all rows parsed to your custom RowProcessor.
settings.setRowProcessor(myDimensionProcessor);
//Finally, we create a parser
CsvParser parser = new CsvParser(settings);
//And parse! All rows are sent to your custom RowProcessor (CsvDimension)
//I'm using a 150MB CSV file with 1.3 million rows.
parser.parse(new FileReader(new File("c:/tmp/worldcitiespop.txt")));
//Nothing else to do. The parser closes the input and does everything for you safely. Let's just get the results:
System.out.println("Columns: " + myDimensionProcessor.lastColumn);
System.out.println("Rows: " + myDimensionProcessor.rowCount);
System.out.println("Time taken: " + (System.currentTimeMillis() - start) + " ms");
}
输出将是:
Columns: 7
Rows: 3173959
Time taken: 1279 ms
披露:我是这个图书馆的作者。它是开源和免费的(Apache V2.0许可证)。
答案 2 :(得分:0)
要查找行数,您必须阅读整个文件。你在这里无能为力。但是,找到cols数量的方法效率有点低。而不是split
只计算多少次&#34;,&#34;出现在线上。您可能还包括@Vlad。
String.split
方法创建了一个字符串数组,并使用regexp进行拆分,效率不高。
答案 3 :(得分:0)
IMO,你正在做的是一种可以接受的方式。但是有一些方法可以让它更快:
答案 4 :(得分:0)
我想这里的选项取决于你如何使用数据:
答案 5 :(得分:0)
我在这里找到了这个简短而有趣的解决方案: https://stackoverflow.com/a/5342096/4082824
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("File1")));
lnr.skip(Long.MAX_VALUE);
System.out.println(lnr.getLineNumber() + 1); //Add 1 because line index starts at 0
lnr.close();
答案 6 :(得分:0)
我的解决方案是使用多行单元格或引用值简单正确地处理CSV。
例如我们有csv-file:
1,"""2""","""111,222""","""234;222""","""""","1
2
3"
2,"""2""","""111,222""","""234;222""","""""","2
3"
3,"""5""","""1112""","""10;2""","""""","1
2"
我的解决方案片段是:
import java.io.*;
public class CsvDimension {
public void parse(Reader reader) throws IOException {
long cells = 0;
int lines = 0;
int c;
boolean qouted = false;
while ((c = reader.read()) != -1) {
if (c == '"') {
qouted = !qouted;
}
if (!qouted) {
if (c == '\n') {
lines++;
cells++;
}
if (c == ',') {
cells++;
}
}
}
System.out.printf("lines : %d\n cells %d\n cols: %d\n", lines, cells, cells / lines);
reader.close();
}
public static void main(String args[]) throws IOException {
new CsvDimension().parse(new BufferedReader(new FileReader(new File("test.csv"))));
}
}