我正在寻找关于如何处理正在创建的csv文件的建议,然后由我们的客户上传,并且可能在值中有逗号,如公司名称。

我们正在考虑的一些想法是:带引号的标识符(value "," values ","等等)或使用|代替逗号。最大的问题是我们必须让它变得简单,否则客户就不会这么做。


当前回答

我发现的最简单的解决方案是LibreOffice使用的:

替换所有“by”字面值 在字符串周围加上双引号

你也可以使用Excel使用的:

替换所有字面的" by " 在字符串周围加上双引号

注意,其他人建议只执行上面的第2步,但这对于“后面跟着一个,”的行不起作用,就像在CSV中,你想要有一个字符串hello”,world的单列,因为CSV会这样读:

"hello",world"

它被解释为有两列的行:hello和world"

其他回答

使用制表符(\t)分隔字段。

您可以使用其他“分隔符”,如“;”或“|”,但最简单的可能只是引用,这是大多数(体面的)CSV库和大多数体面的电子表格所支持的。

有关CSV分隔符的更多信息以及描述分隔符和引用的标准格式的规范,请参阅此网页

如果您对如何解析一般文件(以CSV为例)更有教育意义的练习感兴趣,您可以查看Julian Bucknall的这篇文章。我喜欢这篇文章,因为它把事情分解成更小的问题,这些问题不那么难以克服。首先创建一个语法,一旦您有了一个好的语法,将语法转换为代码是一个相对简单和有条理的过程。

本文使用c#,并在底部有一个下载代码的链接。

正如我在对harpo的回答的评论中提到的,他的解决方案在大多数情况下都很好,但是在某些情况下,当逗号直接相邻时,它无法在逗号上分割。

这是因为Regex字符串意外地表现为vertabim字符串。 为了获得正确的行为,regex字符串中的所有“字符都需要手动转义,而不使用vertabim转义。

Ie。正则表达式应该是这样的,使用手动转义:

",(?=(?:[^\"\"]*\"\"[^\"\"]*\"\")*(?![^\"\"]*\"\"))"

这转化为 ",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"

当使用一个vertabim字符串 @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))" 它表现为以下你可以看到如果你调试正则表达式:

",(?=(?:[^"]*"[^"]*")*(?![^"]*"))"

总之,我推荐harpo的解决方案,但要注意这个小陷阱!

我已经在CsvReader中包含了一些可选的故障保护,以便在发生此错误时通知您(如果您有预先知道的列数):

if (_expectedDataLength > 0 && values.Length != _expectedDataLength) 
throw new DataLengthException(string.Format("Expected {0} columns when splitting csv, got {1}", _expectedDataLength, values.Length));

可以通过构造函数注入:

public CsvReader(string fileName, int expectedDataLength = 0) : this(new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
    _expectedDataLength = expectedDataLength;
}

您可以像这样读取csv文件。

这利用了分割和空格。

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++;
         }