导入CSV文件错误:列值包含列分隔符 [英] Import CSV File Error : Column Value containing column delimiter

查看:227
本文介绍了导入CSV文件错误:列值包含列分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用SSIS将CSV文件导入SQL SERVER

I am trying to Import a Csv File into SQL SERVER using SSIS

这里是一个数据如何看起来的示例

Here's an example how data looks like

Student_Name,Student_DOB,Student_ID,Student_Notes,Student_Gender,Student_Mother_Name
Joseph Jade,2005-01-01,1,Good listener,Male,Amy
Amy Jade,2006-01-01,1,Good in science,Female,Amy
....

Csv列不包含文本限定符(quotations)

Csv Columns are not containing text qualifiers (quotations)

我创建了一个简单的包使用SSIS将其导入到SQL,但有时SQL中的数据如下所示

I Created a simple package using SSIS to import it into SQL but sometime the data in SQL looked like below

Student_Name    Student_DOB Student_ID  Student_Notes   Student_Gender  Student_Mother_Name
Ali Jade    2004-01-01  1   Good listener   Bad in science  Male,Lisa

原因是somtimes [Student_Notes]列包含用作列分隔符的逗号(,),因此行未正确导入

The Reason was that somtimes [Student_Notes] column contains Comma (,) that is used as column delimiter so the Row are not imported Correctly

任何建议

推荐答案

警告:我不是常规的C#编码器。

A word of warning: I'm not a regular C# coder.

但是无论如何,此代码执行以下操作:

But anyway this code does the following:

打开一个名为C:\Input.TXT的文件

It opens a file called C:\Input.TXT

它搜索每一行。如果该行有超过5个逗号,它将从第三个最后一个字段(notes)中取出所有额外的逗号。

It searches each line. If the line has more than 5 commas, it takes all the extra commas out of the third last field (notes)

它将结果写入C:\Output。

It writes the result to C:\Output.TXT - that's the one you need to actually import

可以进行许多改进:


  • 从连接管理器获取文件路径

  • 错误处理

  • 有经验的C#程序员可能会在hlaf中执行代码

  • Get file paths from connection managers
  • Error handling
  • An experienced C# programmer could probably do this in hlaf the code

请注意,您的包需要对相应文件夹的写入权限

Keep in mind your package will need write access to the appropriate folder

public void Main()
{
    // Search the file and remove extra commas from the third last field
    // Extended from code at
    // http://stackoverflow.com/questions/1915632/open-a-file-and-replace-strings-in-c-sharp
    // Nick McDermaid        

    string sInputLine;
    string sOutputLine;
    string sDelimiter = ",";
    String[] sData;
    int iIndex;

    // open the file for read
    using (System.IO.FileStream inputStream = File.OpenRead("C:\\Input.txt"))
    {
        using (StreamReader inputReader = new StreamReader(inputStream))
        {
            // open the output file
            using (StreamWriter outputWriter = File.AppendText("C:\\Output.txt"))
            {
                // Read each line
                while (null != (sInputLine = inputReader.ReadLine()))
                {
                    // Grab each field out
                    sData = sInputLine.Split(sDelimiter[0]);
                    if (sData.Length <= 6)
                    {
                        // 6 or less fields - just echo it out
                        sOutputLine = sInputLine;
                    }
                    else
                    {
                        // line has more than 6 pieces 
                        // We assume all of the extra commas are in the notes field                                

                        // Put the first three fields together
                        sOutputLine =
                            sData[0] + sDelimiter +
                            sData[1] + sDelimiter +
                            sData[2] + sDelimiter;

                        // Put the middle notes fields together, excluding the delimiter
                        for (iIndex=3; iIndex <= sData.Length - 3; iIndex++)
                        {
                            sOutputLine = sOutputLine + sData[iIndex] + " ";
                        }

                        // Tack on the last two fields
                        sOutputLine = sOutputLine +
                            sDelimiter + sData[sData.Length - 2] +
                            sDelimiter + sData[sData.Length - 1];


                    }

                    // We've evaulted the correct line now write it out
                    outputWriter.WriteLine(sOutputLine);
                }
            }
        }
    }


    Dts.TaskResult = (int)Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success;
}

这篇关于导入CSV文件错误:列值包含列分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆