簡體   English   中英

解析此csv文件時出錯

[英]Error parsing this csv file

我正在嘗試解析此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());
    }

你想要這個改變

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

這是編碼問題,請閱讀此內容

要查看錯誤的完整原因,應在catch塊中使用e.printStackTrace()

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM