- Java Data Science Cookbook
- Rushdi Shams
- 247字
- 2021-07-09 18:44:26
Parsing Tab Separated Value (TSV) file using Univocity
Unlike CSV files, Tab Separated Value (TSV) files contain data that is separated by tab delimiters. This recipe shows you how to retrieve data points from TSV files.
Getting ready
In order to perform this recipe, we will require the following:
- Download the Univocity JAR file from http://oss.sonatype.org/content/repositories/releases/com/univocity/univocity-parsers/2.2.1/univocity-parsers-2.2.1.jar. Include the JAR file in your project in Eclipse an external library.
- Create a TSV file from the following data using Notepad. The extension of the file should be
.tsv
. You save the file asC:/testTSV.tsv
:
Year Make Model Description Price 1997 Ford E350 ac, abs, moon 3000.00 1999 Chevy Venture "Extended Edition" 4900.00 1996 Jeep Grand Cherokee MUST SELL!nair, moon roof, loaded 4799.00 1999 Chevy Venture "Extended Edition, Very Large" 5000.00 Venture "Extended Edition" 4900.00
How to do it...
- Create a method named
parseTsv(String)
that takes the name of the file as a String argument:public void parseTsv(String fileName){
- The line separator for the TSV file in this recipe is a newline character or
n
. To set this character as the line separator, modify the settings:settings.getFormat().setLineSeparator("n");
- Using these settings, create a TSV parser:
TsvParser parser = new TsvParser(settings);
- Parse all rows of the TSV file at once as follows:
List<String[]> allRows = parser.parseAll(new File(fileName));
- Iterate over the list object to print/process the rows as follows:
for (int i = 0; i < allRows.size(); i++){ System.out.println(Arrays.asList(allRows.get(i))); }
- Finally, close the method:
}
The full method with the driver method in a class will look like the following:
import java.io.File; import java.util.Arrays; import java.util.List; import com.univocity.parsers.tsv.TsvParser; import com.univocity.parsers.tsv.TsvParserSettings; public class TestTsv { public void parseTsv(String fileName){ TsvParserSettings settings = new TsvParserSettings(); settings.getFormat().setLineSeparator("n"); TsvParser parser = new TsvParser(settings); List<String[]> allRows = parser.parseAll(new File(fileName)); for (int i = 0; i < allRows.size(); i++){ System.out.println(Arrays.asList(allRows.get(i))); } } }
推薦閱讀
- 漫話大數(shù)據(jù)
- 信息系統(tǒng)與數(shù)據(jù)科學(xué)
- 分布式數(shù)據(jù)庫系統(tǒng):大數(shù)據(jù)時代新型數(shù)據(jù)庫技術(shù)(第3版)
- 大數(shù)據(jù):規(guī)劃、實(shí)施、運(yùn)維
- 大數(shù)據(jù)算法
- 達(dá)夢數(shù)據(jù)庫性能優(yōu)化
- Hadoop 3.x大數(shù)據(jù)開發(fā)實(shí)戰(zhàn)
- 中國數(shù)字流域
- 高維數(shù)據(jù)分析預(yù)處理技術(shù)
- Oracle RAC日記
- Power BI智能數(shù)據(jù)分析與可視化從入門到精通
- 數(shù)據(jù)庫技術(shù)及應(yīng)用
- MySQL DBA修煉之道
- 爬蟲實(shí)戰(zhàn):從數(shù)據(jù)到產(chǎn)品
- 數(shù)據(jù)庫與數(shù)據(jù)處理:Access 2010實(shí)現(xiàn)