【问题标题】:Error parsing this csv file解析此 csv 文件时出错
【发布时间】:2018-04-04 23:32:35
【问题描述】:

我正在尝试解析这个 csv 文件,但是当我要打印它时,我得到“输入长度 = 1”作为输出。这是我的代码:谁能解释为什么会这样?

try {
        List<String> lines = Files.readAllLines(Paths.get("src\\exam1_tweets.csv"));
        for(String line : lines) {
            line = line.replace("\"", "");
            System.out.println(line);
        }
    }catch (Exception e) {
        System.out.println(e.getMessage());
    }

【问题讨论】:

  • 预期输出是多少?你得到什么?你只看到一行输出吗?你能至少展示几行 csv 的样子吗?
  • 如果您阅读了完整的异常反馈,您就会知道问题所在。 stackoverflow.com/questions/26268132/…
  • 通过System.out.println(e.getMessage()); 你丢失了很多关于异常的信息。不要那样做。
  • 补充 Gabriel 所说的... e.printStackTrace() 将提供更多有用的信息。你能提供文件的基本内容吗?

标签: java csv parsing


【解决方案1】:

你想要这个改变

List<String> lines = Files.readAllLines(Paths.get("src\\exam1_tweets.csv"),
                StandardCharsets.ISO_8859_1);

这是编码问题,请阅读this

要查看错误的全部原因,您应该在 catch 块中使用 e.printStackTrace()

【讨论】:

    【解决方案2】:

    java代码:

    import java.io.File;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.List;
    
    public class Sof {
    
        public static final String USER_DIR = "user.dir";
    
        public static void main(String[] args) {
            try {
                List<String> lines = Files.readAllLines(
                        Paths.get(System.getProperty(USER_DIR) + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "exam1_tweets.csv"),
                        StandardCharsets.ISO_8859_1);
                for (String line : lines) {
                    line = line.replace("\"", "");
                    System.out.println(line);
                }
            } catch (Exception e) {
                System.out.println("ERROR" + e.getMessage());
            }
    
        }
    
    }
    

    控制台:

    Handle,Tweet,Favs,RTs,Latitude,Longitude
    BillSchulhoff,Wind 3.2 mph NNE. Barometer 30.20 in, Rising slowly. Temperature 49.3 °F. Rain today 0.00 in. Humidity 32%,,,40.76027778,-72.95472221999999
    danipolis,Pausa pro café antes de embarcar no próximo vôo. #trippolisontheroad #danipolisviaja Pause for? https://....,,,32.89834949,-97.03919589
    KJacobs27,Good. Morning. #morning #Saturday #diner #VT #breakfast #nucorpsofcadetsring #ring #college? https://...,,,44.199476,-72.50417299999999
    stncurtis,@gratefuldead recordstoredayus ?????? @ TOMS MUSIC TRADE https://...,,,39.901474,-76.60681700000001
    wi_borzo,Egg in a muffin!!! (@ Rocket Baby Bakery - @rocketbabybaker in Wauwatosa, WI) https://...,,,43.06084924,-87.99830888
    KirstinMerrell,@lyricwaters should've gave the neighbor  a buzz. Iv got ice cream and moms baked goodies ??,,,36.0419128,-75.68186176
    Jkosches86,On the way to CT! (@ Mamaroneck, NY in Mamaroneck, NY) https://.../6rpe6MXDkB,,,40.95034402,-73.74092102
    tmj_pa_retail,We're #hiring! Read about our latest #job opening here: Retail Sales Consultant [CWA MOB] Bryn Mawr PA - https://.../bBwxSPsL4f #Retail,,,40.0230237,-75.31517719999999
    Vonfandango,Me... @ Montgomery Scrap Corporation https://.../kpt7zM4xbL,,,39.10335,-77.13652 ....
    

    【讨论】:

      【解决方案3】:

      我制作了一个 csv 解析器/写入器,由于它的构建器模式而易于使用

      它解析 csv 文件并为您提供 bean 列表

      这里是源代码 https://github.com/i7paradise/CsvUtils-Java8/

      我加入了一个主类 Demo.java 来展示它是如何工作的

      假设您的文件包含此内容

      Item name;price
      "coffe ""Lavazza""";13,99
      "tea";0,00
      "milk
      in three
      lines";25,50
      riz;130,45
      Total;158
      

      你想解析它并将它存储到一个集合中

      class Item {
          String name;
          double price;
          public Item(String name, double p) {
      //      ...
          }
      //...
      }
      

      你可以这样解析:

      List<Item> items = CsvUtils.reader(Item.class)
                      //content of file; or you can use content(String) to give the content of csv as a String
                      .content(new File("path-to-file.csv"))
                      // false to not include the first line; because we don't want to parse the header
                      .includeFirstLine(false)
                      // false to not include the last line; because we don't want to parse the footer
                      .includeLastLine(false)
                      // mapper to create the Item instance from the given line, line is ArrayList<String> that returns null if index not found
                      .mapper(line -> new Item(
                              line.get(0),
                              Item.parsePrice(line.get(1)))
                              )
                      // finally we call read() to parse the file (or the content)
                      .read();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 2016-07-07
        • 1970-01-01
        • 2015-11-18
        • 1970-01-01
        相关资源
        最近更新 更多