понедельник, 23 июня 2014 г.

Reading and Writing CSV Files in C#

By 4 Jul 2012
link



Introduction

A common requirement is to have applications share data with other programs. Although there are interfaces available to work with, for example, Microsoft Excel data files, this approach is generally complex, involves a fair amount of overhead, and requires that support libraries accompany your application.

Comma-Separated Values (CSV) Files

A much simpler way to have your application share data is by reading and writing Comma-Separated Values (CSV) files. CSV files can easily be read and written by many programs, including Microsoft Excel.
For the most part, reading and writing CSV files is trivial. As the name suggestions, a CSV file is simply a plain text file that contains one or more values per line, separated by commas. Each value is a field (or column in a spreadsheet), and each line is a record (or row in a spreadsheet).
However, there is slightly more work involved. Double quotes are used to wrap values that contain commas so that the commas are not interpreted as a value separator. The same is also done for values that contain double quotes. In addition, two double quotes together signify a double quote in the value and not a value separator.
So this seems like a perfect task for a handy little C# class. Listing 1 shows my CsvFileWriter and CsvFileReaderclasses.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ReadWriteCsv
{
    /// <summary>
    /// Class to store one CSV row
    /// </summary>
    public class CsvRow : List<string>
    {
        public string LineText { get; set; }
    }

    /// <summary>
    /// Class to write data to a CSV file
    /// </summary>
    public class CsvFileWriter : StreamWriter
    {
        public CsvFileWriter(Stream stream)
            : base(stream)
        {
        }

        public CsvFileWriter(string filename)
            : base(filename)
        {
        }

        /// <summary>
        /// Writes a single row to a CSV file.
        /// </summary>
        /// <param name="row">The row to be written</param>
        public void WriteRow(CsvRow row)
        {
            StringBuilder builder = new StringBuilder();
            bool firstColumn = true;
            foreach (string value in row)
            {
                // Add separator if this isn't the first value
                if (!firstColumn)
                    builder.Append(',');
                // Implement special handling for values that contain comma or quote
                // Enclose in quotes and double up any double quotes
                if (value.IndexOfAny(new char[] { '"', ',' }) != -1)
                    builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
                else
                    builder.Append(value);
                firstColumn = false;
            }
            row.LineText = builder.ToString();
            WriteLine(row.LineText);
        }
    }

    /// <summary>
    /// Class to read data from a CSV file
    /// </summary>
    public class CsvFileReader : StreamReader
    {
        public CsvFileReader(Stream stream)
            : base(stream)
        {
        }

        public CsvFileReader(string filename)
            : base(filename)
        {
        }

        /// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
                return false;

            int pos = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == '"')
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == '"')
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                        pos++;
                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                    row[rows] = value;
                else
                    row.Add(value);
                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    pos++;
                if (pos < row.LineText.Length)
                    pos++;
            }
            // Delete any unused items
            while (row.Count > rows)
                row.RemoveAt(rows);

            // Return true if any columns read
            return (row.Count > 0);
        }
    }
}
Listing 1: CsvFileWriter and CsvFileReader Classes
Because the .NET stream classes generally seem to be split into reading and writing, I decided to follow that pattern with my CSV class and split it into CsvFileWriter and CsvFileReader. This also simplifies the code because neither class needs to worry about which mode the file is in or protect against the user switching modes.
The writer class performs any encoding necessary, as I described above, and the reader class performs any necessary decoding.

Using the code

Both classes take a CsvRow argument. The CsvRow class derives from List<string>, so it's basically just a list of strings.
When you call CsvFileWriter.WriteRow(), the row argument specifies the string values to write out. And when you call CsvFileReader.ReadRow(), the row argument returns the values that were read in.
CsvFileReader.ReadRow() also places the entire line into the CsvRow.LineText member, just in case the caller wants to inspect that. Finally, CsvFileReader.ReadRow() returns a Boolean value that is false when no values could be read at the current line. Under normal circumstances, this would indicate the end of the file.
Listing 2 demonstrates using the classes.
void WriteTest()
{
    // Write sample data to CSV file
    using (CsvFileWriter writer = new CsvFileWriter("WriteTest.csv"))
    {
        for (int i = 0; i < 100; i++)
        {
            CsvRow row = new CsvRow();
            for (int j = 0; j < 5; j++)
                row.Add(String.Format("Column{0}", j));
            writer.WriteRow(row);
        }
    }
}

void ReadTest()
{
    // Read sample data from CSV file
    using (CsvFileReader reader = new CsvFileReader("ReadTest.csv"))
    {
        CsvRow row = new CsvRow();
        while (reader.ReadRow(row))
        {
            foreach (string s in row)
            {
                Console.Write(s);
                Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
}
Listing 2: Sample Code to Write and Read CSV files

Conclusion 

That's about all there is to it. The classes are fairly simple so I didn't include a sample project. All the code for the classes is shown in Listing 1. Note that this code, as presented, does not handle quoted values that span multiple lines.
This code should be helpful for anyone wanting an easy way to share data with Microsoft Excel or any other program that can read or write CSV files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

воскресенье, 22 июня 2014 г.

Установка git в Windows (на этот раз подробно)

16/12/2009
Логотип GIT
Судя по всему, многие из посетителей приходят на этот блог в поисках руководства по установке Git в Windows. И, что самое печальное, всё что они находят — куцаястраничка со ссылкой на англоязычный скринкаст. Пришло время исправить это недоразумение :)

Установка и настройка

Итак, установка git. Сразу оговорюсь что мы будем ставить msysgit, и заодно произведём необходимые действия для подключения к GitHub. Конечно, можно использовать git и в одиночку, для себя — но здесь, как и с играми, в онлайне намного интереснее :)
Идём на страницу git, в раздел Download и ищем там msysgit для Windows. Сайт git отправляет нас на Google Code. Берём Full Installer for official Git.
Запускаем, устанавливаем. При установке будет предложено выбрать тип запуска Git:
  • Git bash only: git ставится и вызывается командой контекстного меню «Git bash here»/»Git gui here»
  • Run from the Windows command prompt: Устанавливает Git и прописывает путь к консольной версии в PATH. Команду ‘Git Bash here’ всё равно можно использовать.
  • Run Git and tools from Windows Command Prompt: то же что предыдущий вариант, но дополнительно прописывает в Windows путь к различным Unix-утилитам типа find и sort. Git предупреждает нас что при этом вместо windows-приложений с соответствующими именами будут вызываться unix-аналоги
Я предпочитаю второй вариант, т.к. использую git исключительно из командной строки. Так что это руководство будет по большей части консольным :)
Продолжаем установку. В конце git предложит просмотреть файл примечаний к релизу. Собственно, на этом установка заканчивается :) Теперь идём в командную строку (если Вы выбрали этот вариант) и вводим свои данные в git, чтобы он нормально подписывал коммиты.
1
2
git config --global user.name &quot;Ваше имя&quot;
$ git config --global user.email &quot;ваш_email@example.com&quot;
Не забудьте подставить своё имя/ник и email :) Параметр --global говорит нам что мы изменяем глобальные настройки. Чтобы изменить настройки только одного репозитория, перейдите в его папку и сделайте то же без --global:
1
2
3
cd my_repo
git config user.name &quot;Ваш ник&quot;
$ git config user.email &quot;другой_email@example.com&quot;
Кстати, создаётся репозиторий командой git init в нужной папке. Всё, gitможно пользоваться в локальном режиме :)
Давайте теперь что нибудь утянем с Github. Идём туда, делаем поиск или Explore Github, открываем понравившийся проект. Прямо под названием проекта будет Clone URL:
Жмём, копируем команду. Получится примерно что то такое:
1
git clone git://github.com/quickredfox/jquery-builds.git
Переходим в каталог куда мы хотим положить проект, и выполняем команду. Имейте в виду, git создаст для проекта каталог чтобы его туда положить. То есть, если мы выполним эту команду в D:\Source, проект будет в папкеD:\Source\jquery-builds.

Конфигурация для использования GitHub

Чтобы хранить свой проект в GitHub, надо ещё немного покопаться с настройкой:) Нам понадобится пара ключей SSH. Открываем консоль Git bash, всё равно где. В msysgit процесс генерации пары ключей упрощён почти до предела. Делаем:
1
ssh-keygen -t rsa -C "ваш_email@example.com"
У Вас спросят куда положить ключи (не потеряйте их, лучше выбрать предлагаемое программой место), дважды спросят пароль (passphrase). Пароль должен быть сложным. После этого Вы получите два файла и RSA fingerprint примерно такого вида:
1
e8:ae:60:8f:38:c2:98:1d:6d:84:60:8c:9e:dd:47:81 tekkub@gmail.com
Теперь идём и регистрируемся на Гитхабе, в бесплатном варианте.
Внимание, бесплатный аккаунт на GitHub — аккаунт для Open-Source проектов. Вы не сможете закрыть свой код, или скрыть его от других. Не используйте его для проприетарного кода и рабочих проектов!
В поле SSH Public Key вставляем содержимое файла id_rsa.pub, или как Вы его там назвали при создании ключей. Если Вы создали ключи в своей папке пользователя, ssh самостоятельно его найдёт. Иначе, надо будет добавить ключи вручную:
1
ssh-add <путь к файлу ключей>
Завершаем регистрацию. Теперь можно уже проверить что получилось. В простой командной строке подключаемся к серверам github:
1
ssh github.com
В ответ должно прийти:
1
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.
Это значит что всё в порядке.
Если Вы видите No supported authentication methods available, значит Git не может найти программу, способную достучаться до сервера Гитхаба. Строка вызова используемой программы хранится в переменной GIT_SSH. Чтобы использовать программу ssh (самый простой способ), надо сделать в командной строке:
1
set GIT_SSH=ssh
Имейте в виду, после перезагрузки эта переменная вернётся в начальное состояние.
Исходная стать тут: клац