简体   繁体   中英

Java: Parse CSV that is separated by “,” and new line

So I have a CSV that has data in a similar format:

a,b,c,d
e,f,g,h
i,j,k,l

I can parse it by ",", but problem is when I get to the point where d is located, it would parse to this: "de"

How can I work around so it can see d and e as separate? Is there a way to parse for "," or "new line"?

 Scanner scanner;
    try {
        scanner = new Scanner(new File("C:/Users/Desktop/orders.csv"));

        //Set the delimiter used in file
        scanner.useDelimiter(",");

        //Get all tokens and store them in some data structure
        //I am just printing them
        while (scanner.hasNext())
        {
            orders++;
            //System.out.print(scanner.next() + "\n");
            //System.out.println(orders);
            System.out.println("Order = " + scanner.next());
            scanner.next(); //Email
            scanner.next(); //Finacial
            scanner.next(); //Paid at
            scanner.next(); //Fulfillment status
            scanner.next(); //Fulfillment at
            scanner.next(); //Accept
            scanner.next(); //Currency
            scanner.next(); //Subtotal
            System.out.println("SHIPPING: " + scanner.next()); //Shipping
            scanner.next(); //Tax
            scanner.next(); //Total
            scanner.next(); //Discount Code
            scanner.next(); //Discount Amount
            scanner.next(); //Shipping Method
            scanner.next(); //Created at
            scanner.next(); //Lineitem Quantity
            scanner.next(); //Lineitem Name
            scanner.next(); //Lineitem Price
            scanner.next(); //Lineitem compare at price
            scanner.next(); //Lineitem SKU
            scanner.next(); //Lineitem requires shipping
            scanner.next(); //Lineitem Taxable
            scanner.next(); //Lineitem Fulfillment status
            scanner.next(); //Billing Name
            scanner.next(); //Billing Street
            scanner.next(); //Billing address1
            scanner.next(); //Billing address2
            scanner.next(); //Billing Company
            scanner.next(); //Billing City
            scanner.next(); //Billing Zip
            scanner.next(); //Billing providence
            scanner.next(); //Billing country
            scanner.next(); //Billing phone
            scanner.next(); //Shipping name
            scanner.next(); //Ship street
            scanner.next(); //Ship address1
            scanner.next(); //Ship address2
            System.out.println(scanner.next()); //Shipping company
            scanner.next(); //Shipping city
            scanner.next(); //Shipping zip
            scanner.next(); //Shipping providence
            scanner.next(); //Shipping country
            scanner.next(); //Shipping phone
            scanner.next(); //Notes
            System.out.println(scanner.next()); //Notse attributes
            scanner.next(); //TX State Tax
            scanner.next(); //Mckinney Municipal Tax
            System.out.println(scanner.next()); //Cancelled at
            scanner.next(); //Payment method
            System.out.println(scanner.next()); //Payment reference
            scanner.next(); //Refunded amount
            scanner.next(); //Vendor
            scanner.next(); //Id
            System.out.println(scanner.next()); //Tags
            scanner.next(); //Risk level
            scanner.next(); //Source
            System.out.println(scanner.next()); //Lineitem Discount + NEXT order PROBLEM IS HERE!!

            System.out.println("NEXT"); 
        }

        //Do not forget to close the scanner 
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

还要使用定界符“ \\ n”,这样您的令牌将在逗号和换行符处进行拆分。

I suggest that you use an existing parser which would simplify your work.
For example with opencsv you can define your own delimiter, quote character...

If you absolutely want to write you own code, you have to first use the scanner to separate the lines and then split each line:

Scanner scanner = new Scanner(new File("C:/Users/Desktop/orders.csv"));
scanner.useDelimiter("\n");
while (scanner.hasNext()) {
    String[] values = scanner.next().split(",");
    System.out.println(values[0]); // Email
    // Etc.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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