【问题标题】:Replace double quotes (within qualifiers) in CSV for SSIS import替换 CSV 中的双引号(在限定符内)以进行 SSIS 导入
【发布时间】:2012-09-07 14:29:51
【问题描述】:

我有一个从 .csv 文件导入数据的 SSIS 包。该文件中的每个条目都有双引号 (") 限定符,但也有介于两者之间的限定符。我还添加了逗号 (,) 作为列分隔符。我无法向您提供我正在使用的原始数据,但这里是我的数据如何在平面文件源中传递的示例:

"ID-1","A "B"", C, D, E","Today"
"ID-2","A, B, C, D, E,F","Yesterday"
"ID-3","A and nothing else","Today"

如您所见,第二列可以包含引号(和逗号),这会破坏我的 SSIS 导入,并出现指向此行的错误。 我不太熟悉正则表达式,但我听说这在这种情况下可能会有所帮助。

在我看来,我需要将所有双引号 (") 替换为单引号 ('),除了...

  • ...一行开头的所有引号
  • ...一行末尾的所有引号
  • ..."," 中的引用

你们中的任何人都可以帮我解决这件事吗?会很棒!

提前致谢!

【问题讨论】:

  • 您确定B 后面有两个双引号,而它前面只有一个(第一行)吗?这看起来不像是正确的格式。
  • 在 CSV 中,两个双引号代表元素内部的一个双引号。您的格式错误或不是 CSV 格式。正确的例子是"ID-1","A ""B"", C, D, E","Today"
  • 嵌入文本限定符问题已经存在了一段时间。我认为他们在 2012 年的 SSI 中解决了这个问题,但我还没有机会对其进行测试。这是几个解决方案的链接。我通常的方法是在导入之前清理文件的脚本任务(计划 A)。 timlaqua.com/2011/06/…
  • 不幸的是,这正是我需要使用的格式!我已经向客户报告了,但他们告诉我他们无能为力。

标签: regex csv import ssis quote


【解决方案1】:

要根据您的规范将双引号替换为单引号,请使用这个简单的正则表达式。此正则表达式将允许行首和/或行尾有空格。

string pattern = @"(?<!^\s*|,)""(?!,""|\s*$)";
string resultString = Regex.Replace(subjectString, pattern, "'", RegexOptions.Multiline);

这是对模式的解释:

// (?<!^\s*|,)"(?!,"|\s*$)
// 
// Options: ^ and $ match at line breaks
// 
// Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!^\s*|,)»
//    Match either the regular expression below (attempting the next alternative only if this one fails) «^\s*»
//       Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
//       Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
//          Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
//    Or match regular expression number 2 below (the entire group fails if this one fails to match) «,»
//       Match the character “,” literally «,»
// Match the character “"” literally «"»
// Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!,"|\s*$)»
//    Match either the regular expression below (attempting the next alternative only if this one fails) «,"»
//       Match the characters “,"” literally «,"»
//    Or match regular expression number 2 below (the entire group fails if this one fails to match) «\s*$»
//       Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
//          Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
//       Assert position at the end of a line (at the end of the string or before a line break character) «$»

【讨论】:

  • 谢谢!除一种情况外,它都有效。虽然我创建了一个支持多行的正则表达式,但它也将最后一个 " 替换为 ' :-( codeRegex pattern = new Regex("(?code
  • @Yheeky 在最后一个 " 字符之后,您在行尾是否有空格?这是仅在最后一行发生还是在所有文本行上发生?
  • 是的,这就是问题所在。我刚刚删除了每行末尾的 CRLF,现在它可以工作了!谢谢! :)
  • @Yheeky 很好。我已经更新了模式,这样你就可以在线条周围有空格了。
【解决方案2】:

您可以使用正则表达式匹配模式拆分列

/(?:(?<=^")|(?<=",")).*?(?:(?="\s*$)|(?=","))/g

看到这个demo

【讨论】:

    【解决方案3】:

    在加载带有双引号和逗号的 CSV 时,有一个限制是添加了额外的双引号,并且数据也包含在双引号中,您可以在源文件的预览中查看。 因此,添加派生列任务并给出以下表达式:-

    (REPLACE(REPLACE(RIGHT(SUBSTRING(TRIM(COL2),1,LEN(COL2) - 1),LEN(COL2) - 2)," ","@"), "\"\"","\""),"@","")

    粗体部分删除了用双引号括起来的数据。

    试试这个,如果有帮助请告诉我

    【讨论】:

      【解决方案4】:

      对 CSV 目标使用文本限定符 " 在将值插入 CSV 目标之前,添加派生列表达式

      REPLACE(REPLACE([Column1],",",""),"\"","")
      

      这将在您的文本字段中保留"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-14
        相关资源
        最近更新 更多