简体   繁体   English

使用opencsv阅读器解析包含定界符作为列值一部分的txt文件

[英]Parsing txt file which has delimiters as part of column value using opencsv reader

I have a tab delimited text file which I want to parse using openscsv and upload to a database. 我有一个制表符分隔的文本文件,我想使用openscsv解析该文件并将其上传到数据库。 I used CSVReader() to parse the file. 我使用CSVReader()解析文件。 The problem is, some column values have tabs within. 问题是,某些列值中包含选项卡。 For instance, a column ends with a tab, and then it has another tab which is used for separating it from the next column. 例如,一列以一个制表符结尾,然后它具有另一个制表符,用于将其与下一列分开。

I'm having trouble in parsing this file. 我在解析此文件时遇到麻烦。 How do I avoid delimiters which are as part of the value? 如何避免作为值一部分的定界符?

This is the file I'm trying to parse. 是我要解析的文件。 Each line has 2 columns and there are 5 rows in total. 每行有2列,总共5行。 The first row is the header. 第一行是标题。 However, when I parse it using the following code, I get only 3 rows: 但是,当我使用以下代码解析它时,我仅得到3行:

CSVReader reader = new CSVReader(new FileReader("input.txt"), '\t');
String[] nextLine;
int cnt = 0;
while ((nextLine = reader.readNext()) != null) {
    if (nextLine != null) {
        cnt++;
        System.out.println("Length of row "+cnt+" = "+nextLine.length);
        System.out.println(Arrays.toString(nextLine));
    }
}

******** Update ******** ********更新********

Doing a normal readline such as below prints 5 lines: 进行如下所示的正常读取行会打印5行:

BufferedReader br = new BufferedReader(new FileReader("input.txt"));
int lines = 0;
while(br.readLine() != null){
    lines++;
}
System.out.println(lines);
  1. Put quotes on your data - here is a modified unit test from CSVReaderTest that shows quotes will work: 在数据上加上引号-这是CSVReaderTest的修改后的单元测试,显示引号可以工作:

     @Test public void testSkippingLinesWithDifferentEscape() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("Skip this line?t with tab").append("\\n"); // should skip this sb.append("And this line too").append("\\n"); // and this sb.append("a\\t'b\\tb\\tb'\\t'c'").append("\\n"); // single quoted elements CSVReader c = new CSVReader(new StringReader(sb.toString()), '\\t', '\\'', '?', 2); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals("b\\tb\\tb", nextLine[1]); assertEquals("c", nextLine[2]); } 

If that does not work please post some of the lines from your input.txt. 如果这样不起作用,请从您的input.txt中发布一些行。 When I click on the link it takes me to some website trying to sell me a dropbox clone. 当我点击链接时,它会带我去一些网站,试图向我出售一个保管箱克隆品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM