Dealing with commas in a CSV file

后端 未结 27 2813
傲寒
傲寒 2020-11-21 06:53

I am looking for suggestions on how to handle a csv file that is being created, then uploaded by our customers, and that may have a comma in a value, like a company name.

相关标签:
27条回答
  • 2020-11-21 07:31

    Use a tab character (\t) to separate the fields.

    0 讨论(0)
  • 2020-11-21 07:32

    You can read the csv file like this.

    this makes use of splits and takes care of spaces.

    ArrayList List = new ArrayList();
    static ServerSocket Server;
    static Socket socket;
    static ArrayList<Object> list = new ArrayList<Object>();
    
    
    public static void ReadFromXcel() throws FileNotFoundException
    {   
        File f = new File("Book.csv");
        Scanner in = new Scanner(f);
        int count  =0;
        String[] date;
        String[] name;
        String[] Temp = new String[10];
        String[] Temp2 = new String[10];
        String[] numbers;
        ArrayList<String[]> List = new ArrayList<String[]>();
        HashMap m = new HashMap();
    
             in.nextLine();
             date = in.nextLine().split(",");
             name = in.nextLine().split(",");
             numbers = in.nextLine().split(",");
             while(in.hasNext())
             {
                 String[] one = in.nextLine().split(",");
                 List.add(one);
             }
             int xount = 0;
             //Making sure the lines don't start with a blank
             for(int y = 0; y<= date.length-1; y++)
             {
                 if(!date[y].equals(""))
                 {   
                     Temp[xount] = date[y];
                     Temp2[xount] = name[y];
                     xount++;
                 }
             }
    
             date = Temp;
             name =Temp2;
             int counter = 0;
             while(counter < List.size())
             {
                 String[] list = List.get(counter);
                 String sNo = list[0];
                 String Surname = list[1];
                 String Name = list[2];
                 for(int x = 3; x < list.length; x++)
                 {           
                     m.put(numbers[x], list[x]);
                 }
                Object newOne = new newOne(sNo, Name, Surname, m, false);
                 StudentList.add(s);
                 System.out.println(s.sNo);
                 counter++;
             }
    
    0 讨论(0)
  • 2020-11-21 07:32
        public static IEnumerable<string> LineSplitter(this string line, char 
             separator, char skip = '"')
        {
            var fieldStart = 0;
            for (var i = 0; i < line.Length; i++)
            {
                if (line[i] == separator)
                {
                    yield return line.Substring(fieldStart, i - fieldStart);
                    fieldStart = i + 1;
                }
                else if (i == line.Length - 1)
                {
                    yield return line.Substring(fieldStart, i - fieldStart + 1);
                    fieldStart = i + 1;
                }
    
                if (line[i] == '"')
                    for (i++; i < line.Length && line[i] != skip; i++) { }
            }
    
            if (line[line.Length - 1] == separator)
            {
                yield return string.Empty;
            }
        }
    
    0 讨论(0)
提交回复
热议问题