【问题标题】:Can't Separate Text File By Delimiter |无法通过分隔符分隔文本文件 |
【发布时间】:2026-01-12 20:05:01
【问题描述】:

我正在使用 C#。

我正在尝试将文本文件拉入对象。我正在使用 ODBC 连接,它看起来像这样

Driver={Microsoft 文本驱动程序 (*.txt; *.csv)};Dbq=C:\Users\Owner\Desktop\IR\IR_Files\Absolute;Extensions=asc,csv,tab,txt;

我可以建立连接,但我无法将我的列分开。我正在使用 schema.ini 文件,但它不起作用。这是我的架构文件。

[MyTextFile.CSV]
格式=分隔(|)
ColNameHeader=假
Col1=fullstockn 文本
col2=FULLINFO 文本
MaxScanRows=0
字符集=ANSI

文本文件如下所示。

fullstockn|FULLINFO

“555555”|

继续 : Neuf Ttudes sur l 这里还有一些文字.....

【问题讨论】:

标签: c# text-files flat-file csv


【解决方案1】:

您是否有理由为此需要使用 ODBC 连接?我认为直接打开文本文件并自己解析会更容易。

【讨论】:

  • 如何更容易?有什么简单的方法?
【解决方案2】:

我不知道这是否重要,但是...

您的 dbq 属性中可能缺少结尾“\”...

编辑:实际上...在您发布的文本中,您有 3 列,而不是 2...(2 个管道而不是 1 个)

【讨论】:

    【解决方案3】:

    我总是自己为这种操作编写代码。这是我不久前为此目的编写的一个抽象类的示例。如果您愿意,可以对其进行修改或子类化

    public abstract class ReadTextFile : ILoadable
    {
        public string Path { get; set; }
        public UploadFileType FileType { get; set; }
        protected internal List<List<string>> Table { get; set; }
        public Guid BatchID { get; set; }
    
        /// <summary>
        /// Method that loads the raw text into a collection of strings
        /// </summary>
        /// <returns></returns>
        public bool Load()
        {
            Table = new List<List<string>>();
            var splitter = Convert.ToChar("\t");
            try
            {
                using (TextReader tr = new StreamReader(Path))
                {
                    // Discard the first line
                    String line = tr.ReadLine();
    
                    // Read and display lines from the file until the end of the file is reached.
                    while ((line = tr.ReadLine()) != null)
                    {
                        Table.Add(line.Split(splitter).ToList<string>());
                    }
                    tr.Close();
                    tr.Dispose();
                }
                return true;
    
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        public string Left(string param, int length)
        {
            //we start at 0 since we want to get the characters starting from the
            //left and with the specified lenght and assign it to a variable
            string result = param.Substring(0, length);
            //return the result of the operation
            return result;
        }
        public string Right(string param, int length)
        {
            //start at the index based on the lenght of the sting minus
            //the specified lenght and assign it a variable
            string result = param.Substring(param.Length - length, length);
            //return the result of the operation
            return result;
        }
    }
    

    【讨论】:

      【解决方案4】:

      尝试使用此连接字符串

      Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Owner\Desktop\IR\IR_Files\Absolute\MyTextFile.CSV;Extended Properties='text'
      

      和:

      • 注意列数
      • 将 schema.ini 放在可执行文件的同一文件夹中。

      【讨论】:

        【解决方案5】:

        我使用下面的连接字符串

        string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"text;HDR=YES;Format=Delimited(|)\";", Path.GetDirectoryName(path));
        

        还有一个通常开始的 Schema.ini 文件

        [myFile.txt]
        Format=Delimited(|)
        TextDelimiter="none"
        

        我将通过

        执行阅读器
        command.CommandText = String.Format("SELECT * FROM [{0}]", Path.GetFileName(path));
        OleDbDataReader reader = command.ExecuteReader();
        

        此外,当我第一次调查此问题时,文本文件驱动程序上的 MSDN page 很有帮助。具体来说,Schema.ini 文件上的page 非常有用。

        【讨论】:

        • TextDelimiter="none" 是说没有文本分隔符,还是实际上将分隔符设置为字符串 "none"?我在导入带有嵌入引号的文件时遇到了问题,想知道是否有比将TextDelimiter 设置为我认为文件中不会出现的某个字符更好的方法。