Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向csv文件添加双引号_C#_.net_Regex_Csv - Fatal编程技术网

C# 向csv文件添加双引号

C# 向csv文件添加双引号,c#,.net,regex,csv,C#,.net,Regex,Csv,我有csv文件,我应该做的是添加双引号,如果单元格不包含它,否则不包含它。这是我的C代码 foreach(csv.GetFieldHeaders()中的变量项) { dataColumn=dataColumn+“\”“+项+”\“+”,”; } WriteLine(dataColumn.Substring(0,dataColumn.Length-1)); dataColumn=string.Empty; //迭代循环遍历文件中的所有行,并在其中添加引号 字符串str; 而(csv.ReadNex

我有csv文件,我应该做的是添加双引号,如果单元格不包含它,否则不包含它。这是我的C代码

foreach(csv.GetFieldHeaders()中的变量项)
{
dataColumn=dataColumn+“\”“+项+”\“+”,”;
}
WriteLine(dataColumn.Substring(0,dataColumn.Length-1));
dataColumn=string.Empty;
//迭代循环遍历文件中的所有行,并在其中添加引号
字符串str;
而(csv.ReadNextRecord())
{
对于(int i=0;i
现在我有一个手机 “打开锚定标记href=”http://abc.html“target=“\u blank”>xyz关闭锚定标记, 当csv[i]是此数据时,我收到一个错误,无法找到适合指定区域性或中性区域性的任何资源。确保CsvReader.Resources.ExceptionMessage.Resources“已在编译时正确嵌入或链接到程序集“CsvReader”,或者确保所需的所有附属程序集都是可加载和完全签名的。 文件G:data.CSV已在0秒内成功解析 按任意键继续

但是如果我有一个字段,比如(删除链接的双引号后) “打开锚定标记href=target=\u blank>xyz关闭锚定标记
然后它就起作用了。请在不删除双引号的情况下提供上述解决方案。

这可能会引起兴趣:ps。还有一件事需要注意-一些csv格式允许在带引号的字符串中使用新行字符,有些不允许。您使用的是什么具体的
CsvReader
?您是说如果原始行包含嵌入引号,然后CsvReader抛出一个异常?或者你是说试图读取转换后的行会引发异常?还是你说的完全不同?错误发生在哪一行?修改您的数据比修改其他人的代码更容易。
 foreach (var item in csv.GetFieldHeaders())
                {

                    dataColumn = dataColumn + "\"" + item + "\"" + ',';
                }
                writer.WriteLine(dataColumn.Substring(0, dataColumn.Length - 1));
                dataColumn = string.Empty;
                //Iteratively loop through all the Rows in the File and Append quotes to it
                string str;

                while (csv.ReadNextRecord())
                {
                    for (int i = 0; i < csv.FieldCount; i++)
                    {
                        str = csv[i];
                        if (csv[i].Length == 0)//if the column don't have any data
                        {
                            dataColumn = dataColumn + csv[i] + ",";
                        }

                        else
                            dataColumn = dataColumn + "\"" + csv[i].Replace("\"", "'").Replace("\n", " ").Replace("\r", "").Replace(" ", "()").Replace(")(", "").Replace("()", " ") + "\"" + ",";
                    }
                    row.Append(dataColumn.Substring(0, (dataColumn.Length) - 1));//-1 to exclude the comma (,) at the end of the row
                    writer.WriteLine(row);//Write to File
                    dataColumn = null;
                    row.Clear();
                }

            }