在“SSIS控制流脚本任务”中,从CSV文件开始删除行

本文关键字:CSV 文件 删除行 开始 SSIS 控制流 脚本 任务 | 更新日期: 2023-09-27 17:49:34

我有一个.csv文件,看起来像这样:

#Example Company                        
#(999) 999-9999                      
#http://yourwebsite.com                             
#Report Date Range: Dec 26, 2013 - Dec 26, 2013                     
#Exported: Dec 26, 2013                             
#Twitter : Profile Summary                              
#Screen Name,Name,Description,Location,Followers,Following,Listed
SctaSa,statisticalgraph,statistical Screen- The official account for your 
organization,Saudi Arabia,6775,8,75

所以,我需要从.csv文件中获取特定的数据,以便SSIS转换可读,从以#开头的列"Screen Name"remove the garbage data开始,看起来像

Screen Name,Name,Description,Location,Followers,Following,Listed,Exported,Report Date Range
SctaSa,statisticalgraph,statistical Screen- The official account for your organization,Saudi Arabia,6775,8,75,26-Dec-13,26-Dec-13

我试图使用这个c#脚本,但它不穿文件(我不是c#专家,所以我不知道问题是什么)我试图使用以下脚本删除任何行以# but the file dose not transfare to the out put path开头;你能给我一些建议吗?

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
#endregion
namespace ST_a7b941606e0b40aa920bfe13fc81dc81
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var lines = new List<string>();
            string line;
            using (var file = new System.IO.StreamReader("D:''try.csv")) 
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Length != 0)
                    {
                        if (!line.StartsWith("#")  )
                        {
                            lines.Add(line);
                        }
                    }
                } 
           }
           File.WriteAllLines("D:''SCTA_ETL''try.csv", lines);
        }
        /// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
        public void Main()
        {
            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
        }
        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
    }
}

在“SSIS控制流脚本任务”中,从CSV文件开始删除行

另一种说法:

File.WriteAllLines(outputPath, File.ReadAllLines("c:''mycsv.csv").Where(x => !x.StartsWith("#")).ToArray());

您可能需要在中间更改逻辑:

var lines = new List<string>();
string outputPath = // your output path here
using (var file = new System.IO.StreamReader("c:''mycsv.csv")) 
{
  string line;
  while ((line = file.ReadLine()) != null)
  {
    if (!line.StartsWith("#"))
    {
      lines.Add(line);
    }
  } 
}
File.WriteAllLines(outputPath, lines);

您已经删除了所有内部带有"#"的行。

相反,只添加不以"#"开头的行。

另外,当您完成StreamReader时,一定要关闭并处理它,或者只是将整个内容放在using部分中。