试图修复文件解析器

时间:2013-07-20 22:03:55

标签: .net vb.net text visual-studio-2012

所以我作为数据分析师工作,似乎IT部门从来没有时间对我的一些项目进行编码,所以我决定自己做!我做了一个非常基本的程序,它接受一个源文件(.csv)并在列中移动到我需要的顺序。它工作得很好,但我需要一些帮助。

我很清楚可能有更好的方法来完成所有这些。我不是100%设置我的代码,特别是如果它不能做我想要的下面。

  1. 我想在编写完成后弹出一些消息框,但我无法弄明白该怎么做。

    I figured out a way to do this.  I just added the revised code below.  
    
  2. 我想用“ - ”作为分隔符解析邮政编码,然后将“ - ”后的所有内容移到下一栏(所以我可以将zip和zip + 4作为不同的列)。正如你所看到的,我试图做类似的事情,但它没有用。 (Zip在第8栏)

  3. 我需要插入带有某些信息的标题行。我不知道怎么回事!

    I figured this out too!  Edited the code below again.  
    
  4. 我还想更改日期格式。源文件是MM / DD / YYYY,但我想以YYYYMMDD的形式发送。 (这将是第10和第11栏)

  5. 所以,这是我到目前为止的代码。它工作得很好,但没有做我想要的所有东西。

     Public Sub ParseButton_Click(sender As Object, e As EventArgs) Handles ParseButton.Click
    
        ' Create the IEnumerable data source. 
        Dim lines As String() = System.IO.File.ReadAllLines(FileInLocation).Skip(1).ToArray()
        Dim Date1 As Date = Date.Now
    
        Dim lineQuery = From line In lines
                 Let x = line.Split(New Char() {","})
                 Select "10010" & "," & (Date1.ToString("yyyyMMdd")) & "," &
                  (Date1.ToString("yyyyMMdd")) & "," &
                  "WY Mental Health Professions Licensing Board" & "," &
                 "WY" & "," & x(4) & " " & "," & "," & x(1) & "," & x(2) & "," & x(0) & "," &
                 x(5) & "," & "" & "," & x(6) & "," & x(7) & " " & "," & "," & x(8) & "," & " " & "," &
                 x(3) & "," & x(9) & " " & "," & "," & x(10) & "," & x(11) & "," & x(12)
    
        ' Execute the query and write out the new file. Note that WriteAllLines 
        ' takes a string array, so ToArray is called on the query.
    
        Dim inputString As String = "DartID,DateAdded,DateUpdated,Website,State,BusinessName,DBA,NameFirst,NameMiddle,NameLast,NameSuffix,Address,AddressLine2,AddressCity,AddressState,AddressZipCode,Zip4,Email,LicenseNo,LicenseType,LicenseDateFrom,LicenseDateTo,LicenseStatus,"
    
        System.IO.File.WriteAllText(FileOutLocation & "\Output.csv", inputString)
    
        System.IO.File.AppendAllText(FileOutLocation & "\Output.csv", Environment.NewLine)
    
        System.IO.File.AppendAllLines(FileOutLocation & "\Output.csv", lineQuery.ToArray())
    
        'Show message box on completion of file write
    
        If My.Computer.FileSystem.FileExists(FileOutLocation & "\Output.csv") Then
            MsgBox("Parse Complete!")
        Else
            MsgBox("Error, Parse Failed!")
        End If
    
    End Sub
    

    感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我还想更改日期格式。源文件是MM / DD / YYYY,但我想以YYYYMMDD的形式发送。 (这将是第10和第11列)

你可以打开临时字符串, 例如:

    string *temp = Date1.tostring();
    string temp2[Date1.tostring().length] ;

并应对它

     temp2[0]=temp[4+2];
     temp2[1]=temp[5+2];

等 <或p>

     for(int i=0; i<4;i++)
     temp2[i]=temp[i+4+2];

天和monthes相同

不要忘记在每种格式之后加上斜杠

抱歉,我不知道这种语言的语法,但我打赌你可以搞清楚。

答案 1 :(得分:0)

日期可以简单地完成,就像这个DateTime.Parse(x(10))。ToString(“yyyyMMdd”)