Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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#_Csv - Fatal编程技术网

C#CSV用制表符替换分隔符-逗号(数据可能包含逗号)

C#CSV用制表符替换分隔符-逗号(数据可能包含逗号),c#,csv,C#,Csv,我想在CSV文件中将分隔符逗号替换为制表符 输入 输出 请注意,引号中的单词不应替换逗号。同样在输出中,我们希望省略双引号 我尝试了以下方法,但代码也用引号括起来的单词替换逗号 public void Replace_comma_with_tabs(string path) { var file = File .ReadLines(path) .SkipWhile(line => string.IsNullOrWhiteSp

我想在CSV文件中将分隔符逗号替换为制表符

输入

输出

请注意,引号中的单词不应替换逗号。同样在输出中,我们希望省略双引号

我尝试了以下方法,但代码也用引号括起来的单词替换逗号

public void Replace_comma_with_tabs(string path)
{
   var file = File
              .ReadLines(path)
              .SkipWhile(line => string.IsNullOrWhiteSpace(line)) // To be on the safe side
              .Select((line, index) => line.Replace(',', '\t')) // replace ',' with '\t'
              .ToList();                  // Materialization, since we write into the same file
    
   File.WriteAllLines(path, file);
}

如何跳过引号括起来的单词的逗号?

有很多方法可以做到这一点,但这里有一种。这仅包括用于转换包含逗号分隔文本和引号文本的字符串的代码。在Select语句中使用“ToTabs”而不是“Replace”。您必须对此进行强化,以添加一些错误检查

这将处理引号字段内的转义引号,并将现有选项卡转换为空格,但它不是一个完整的CSV解析器

static class CsvHelper
{
    public static string ToTabs(this string source)
    {
        Func<char,char> getState = NotInQuotes;
        char last = ' ';

        char InQuotes(char ch)
        {
            if ('"' == ch && last != '"')
                getState = NotInQuotes;
            else if ('\t' == ch)
                ch = ' ';

            last = ch;

            return ch;
        }

        char NotInQuotes(char ch)
        {
            last = ch;

            if ('"' == ch)
                getState = InQuotes;
            else if (',' == ch)
                return '\t';
            else if ('\t' == ch)
                ch = ' ';

            return ch;
        }
        return string.Create(source.Length, getState, (buffer,_) =>
        {
            for (int i = 0; i < source.Length; ++i)
            {
                buffer[i] = getState(source[i]);
            }
        });
    }
}

    static void Main(string[] _)
    {
        const string Source = "a,string,with,commas,\"field,with,\"\"commas\", and, another";

        var withTabs = Source.ToTabs();

        Console.WriteLine(Source);
        Console.WriteLine(withTabs);
    }
静态类CsvHelper
{
公共静态字符串ToTabs(此字符串源)
{
Func getState=NotInQuotes;
char last='';
char-InQuotes(char-ch)
{
如果(“”==ch&&last!=“”)
getState=NotInQuotes;
else if('\t'==ch)
ch='';
last=ch;
返回ch;
}
char NotInQuotes(char ch)
{
last=ch;
如果(“”==ch)
getState=InQuotes;
else如果(','==ch)
返回'\t';
else if('\t'==ch)
ch='';
返回ch;
}
返回string.Create(source.Length,getState,(buffer,))=>
{
对于(int i=0;i
有很多方法可以做到这一点,但这里有一种。这只包括将逗号分隔的文本转换为带引号的文本的字符串的代码。在Select语句中使用“ToTabs”而不是“Replace”。您必须对此进行强化,以添加一些错误检查

这将处理引号字段内的转义引号,并将现有选项卡转换为空格,但它不是一个完整的CSV解析器

static class CsvHelper
{
    public static string ToTabs(this string source)
    {
        Func<char,char> getState = NotInQuotes;
        char last = ' ';

        char InQuotes(char ch)
        {
            if ('"' == ch && last != '"')
                getState = NotInQuotes;
            else if ('\t' == ch)
                ch = ' ';

            last = ch;

            return ch;
        }

        char NotInQuotes(char ch)
        {
            last = ch;

            if ('"' == ch)
                getState = InQuotes;
            else if (',' == ch)
                return '\t';
            else if ('\t' == ch)
                ch = ' ';

            return ch;
        }
        return string.Create(source.Length, getState, (buffer,_) =>
        {
            for (int i = 0; i < source.Length; ++i)
            {
                buffer[i] = getState(source[i]);
            }
        });
    }
}

    static void Main(string[] _)
    {
        const string Source = "a,string,with,commas,\"field,with,\"\"commas\", and, another";

        var withTabs = Source.ToTabs();

        Console.WriteLine(Source);
        Console.WriteLine(withTabs);
    }
静态类CsvHelper
{
公共静态字符串ToTabs(此字符串源)
{
Func getState=NotInQuotes;
char last='';
char-InQuotes(char-ch)
{
如果(“”==ch&&last!=“”)
getState=NotInQuotes;
else if('\t'==ch)
ch='';
last=ch;
返回ch;
}
char NotInQuotes(char ch)
{
last=ch;
如果(“”==ch)
getState=InQuotes;
else如果(','==ch)
返回'\t';
else if('\t'==ch)
ch='';
返回ch;
}
返回string.Create(source.Length,getState,(buffer,))=>
{
对于(int i=0;i
这里有一种方法。它使用标志
quotesStarted
检查逗号是否应被视为分隔符或列中文本的一部分。我还使用了
StringBuilder
,因为该类具有良好的字符串连接性能。它读取行,然后对每行重复其字符,并检查具有特殊含义的字符(逗号、单引号、制表符、单引号之间的逗号):


这里有一种方法。它使用标志
quotesStarted
检查逗号是否应被视为分隔符或列中文本的一部分。我还使用了
StringBuilder
,因为该类具有良好的字符串连接性能。它读取行,然后对每行重复其字符,并检查具有特殊含义的字符(逗号、单引号、制表符、单引号之间的逗号):


要将字符串中的逗号更改为制表符,请使用
Replace
方法

例如:

str2.Replace(",", "hit tab key");

string str = "Lucy, John, Mark, Grace";
string str2 = str.Replace(",", "    ");

要将字符串中的逗号更改为制表符,请使用
Replace
方法

例如:

str2.Replace(",", "hit tab key");

string str = "Lucy, John, Mark, Grace";
string str2 = str.Replace(",", "    ");

@CodeCaster引用字段中的单引号无效。@CodeCaster引用字段中的单引号无效。