解析此csv文件时出错

时间:2018-03-02 16:23:23

标签: java csv parsing

我正在尝试解析这个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());
    }

3 个答案:

答案 0 :(得分:0)

你想要这个改变

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

编码问题请阅读this

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

答案 1 :(得分:0)

我已经制作了一个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();

答案 2 :(得分:0)

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 ....