-2

ファイルのセットをループして、カンマで区切られているかどうかを確認する必要があります.C#.私はC#が初めてです.これで私を助けてください.

前もって感謝します。

4

2 に答える 2

0

これは、ディレクトリを取得し、ディレクトリをスキャンしてすべてのファイルを探し、それらを繰り返し処理して、コンマを含む行のパーセンテージを返す簡単なコンソール アプリです。指摘されているように、検証できる CSV ライブラリがあります。これは、開始するための簡単な例です。

これを使用するには、Visual Studio で新しいコンソール アプリ プロジェクトを作成し、「TestStub」という名前を付けてから、これをコピーして「Program.cs」ファイルに貼り付けます。

namespace TestStub
{
    using System;
    using System.IO;
    using System.Text;

    public class Program
    {
        private static char[] CSV = { ',', ',' };
        private static bool csvFound = false;
        /// <summary>
        /// This is the console program entry point
        /// </summary>
        /// <param name="args">A list of any command-line args passed to this application when started</param>
        public static void Main(string[] args)
        {
            // Change this to use args[0] if you like 
            string myInitialPath = @"C:\Temp";
            string[] myListOfFiles;

            try
            {
                myListOfFiles = EnumerateFiles(myInitialPath);

                foreach (string file in myListOfFiles)
                {
                    Console.WriteLine("\nFile {0} is comprised of {1}% CSV delimited lines.",
                        file, 
                        ScanForCSV(file));
                }

                Console.WriteLine("\n\nPress any key to exit.");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(
                    "Error processing {0} for CSV content: {1} :: {2}", 
                    myInitialPath, 
                    ex.Message, 
                    ex.InnerException.Message);
            }
        }

        /// <summary>
        /// Get a list of all files for the specified path
        /// </summary>
        /// <param name="path">Directory path</param>
        /// <returns>String array of files (with full path)</returns>
        public static string[] EnumerateFiles(string path)
        {
            string[] arrItems = new string[1];

            try
            {
                arrItems = Directory.GetFiles(path);
                return arrItems;
            }
            catch (Exception ex)
            {
                throw new System.IO.IOException("EnumerateFilesAndFolders() encountered an error:", ex);
            }
        }
        /// <summary>
        /// Determines if the supplied file has comma separated values
        /// </summary>
        /// <param name="filename">Path and filename</param>
        /// <returns>Percentage of lines containing CSV elements -vs- those without</returns>
        public static float ScanForCSV(string filename)
        {
            //
            // NOTE: You should look into one of the many CSV libraries
            // available. This method will not carefully scruitinize
            // the file to see if there's a combination of delimeters or
            // even if it's a plain-text (e.g. a newspaper article)
            // It just looks for the presence of commas on multiple lines
            // and calculates a percentage of them with and without
            //
            float totalLines = 0;
            float linesCSV = 0;

            try
            {
                using (StreamReader sReader = new StreamReader(filename))
                {
                    int elements = 0;
                    string line = string.Empty;
                    string[] parsed = new string[1];

                    while (!sReader.EndOfStream)
                    {
                        ++totalLines;
                        line = sReader.ReadLine();
                        parsed = line.Split(CSV);
                        elements = parsed.Length;
                        if (elements > 1)
                        {
                            ++linesCSV;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new System.IO.IOException(string.Format("Problem accessing [{0}]: {1}", filename, ex.Message), ex);
            }
            return (float)((linesCSV / totalLines) * 100);
        }
    }  
}

}

于 2012-08-14T16:15:21.947 に答える
0

誰かがすでに指摘しているように、それは簡単な解決策ではありません。すべてのコンマ区切りファイルに指定された拡張子 (csv など) があれば、非常に簡単ですそうでない場合は、次のアルゴリズムが機能するはずです。

  1. 指定したディレクトリ内のファイルのすべての名前 (パス + 名) を取得します。必要に応じて、関心のあるものだけをフィルタリングします。ヒント: System.IO.Directoryand System.IO.Fileand and System.IO.DirectoryInfoand を見てくださいSystem.IO.FileInfo
  2. すべてのファイルを調べて、カンマで区切られているかどうかを確認する必要があります。これはトリッキーな部分になります。ファイルの各行をチェックし、カンマで区切られているかどうかを通知する正規表現を作成できます。

正規表現は、最初は習得するのが少し難しいですが、しばらくすると元に戻るはずです。

于 2012-08-14T15:13:58.177 に答える