我正在寻找关于如何处理正在创建的csv文件的建议,然后由我们的客户上传,并且可能在值中有逗号,如公司名称。
我们正在考虑的一些想法是:带引号的标识符(value "," values ","等等)或使用|代替逗号。最大的问题是我们必须让它变得简单,否则客户就不会这么做。
我正在寻找关于如何处理正在创建的csv文件的建议,然后由我们的客户上传,并且可能在值中有逗号,如公司名称。
我们正在考虑的一些想法是:带引号的标识符(value "," values ","等等)或使用|代替逗号。最大的问题是我们必须让它变得简单,否则客户就不会这么做。
当前回答
如果您在*nix-系统上,可以访问sed,并且仅在您的CSV的特定字段中可以有一个或多个不需要的逗号,您可以使用以下一行程序,以便将它们包含在RFC4180 Section 2中:
sed -r 's/([^,]*,[^,]*,[^,]*,)(.*)(,.*,.*)/\1"\2"\3/' inputfile
根据不需要的逗号可能在哪个字段中,您必须更改/扩展正则表达式的捕获组(以及替换)。 上面的示例将第四个字段(六个字段中的第四个字段)括在引号中。
结合使用——In -place选项,您可以将这些更改直接应用到文件。
为了“构建”正确的正则表达式,需要遵循一个简单的原则:
对于CSV中出现在不需要的逗号字段之前的每个字段,您可以编写一个[^,]*,并将它们放在一个捕获组中。 对于包含不需要的逗号的字段,请写入(.*)。 对于带不需要的逗号的字段后面的每个字段,都要写一个,。*并将它们放在一个捕获组中。
下面是根据特定字段的不同可能的正则表达式/替换的简短概述。如果没有给出,则替换为\1"\2"\3。
([^,]*)(,.*) #first field, regex
"\1"\2 #first field, substitution
(.*,)([^,]*) #last field, regex
\1"\2" #last field, substitution
([^,]*,)(.*)(,.*,.*,.*) #second field (out of five fields)
([^,]*,[^,]*,)(.*)(,.*) #third field (out of four fields)
([^,]*,[^,]*,[^,]*,)(.*)(,.*,.*) #fourth field (out of six fields)
如果您想用sed删除不需要的逗号,而不是用引号将它们括起来,请参考此答案。
其他回答
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;
}
}
您可以在字段周围加上双引号。我不喜欢这种方法,因为它增加了另一个特殊字符(双引号)。只需定义一个转义字符(通常是反斜杠),并在需要转义的地方使用它:
data,more data,more data\, even,yet more
您不必尝试匹配引号,而且需要解析的异常也更少。这也简化了您的代码。
如果你想重新发明轮子,下面的方法可能对你有用:
public static IEnumerable<string> SplitCSV(string line)
{
var s = new StringBuilder();
bool escaped = false, inQuotes = false;
foreach (char c in line)
{
if (c == ',' && !inQuotes)
{
yield return s.ToString();
s.Clear();
}
else if (c == '\\' && !escaped)
{
escaped = true;
}
else if (c == '"' && !escaped)
{
inQuotes = !inQuotes;
}
else
{
escaped = false;
s.Append(c);
}
}
yield return s.ToString();
}
您可以像这样读取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++;
}
由于这是关于一般实践,让我们从经验法则开始:
不要使用CSV,而是使用带有库的XML来读写XML文件。 如果必须使用CSV。正确操作并使用免费库来解析和存储CSV文件。
为了证明1),大多数CSV解析器没有编码意识,所以如果你不处理US-ASCII,你是在自找麻烦。 例如,excel2002以本地编码存储CSV,而没有任何关于编码的说明。CSV标准没有被广泛采用:(。 另一方面,xml标准被很好地采用,它处理编码非常好。
为了证明2),几乎所有语言都有大量的csv解析器,所以即使解决方案看起来非常简单,也没有必要重新发明轮子。
举几个例子:
对于python使用构建CSV模块 为perl检查CPAN和文本::CSV 对于PHP,使用内置的fgetcsv/fputcsv函数 java检查SuperCVS库
实际上,如果你不打算在嵌入式设备上解析它,就没有必要手动实现它。