2013-03-21 2 views
-1

내 텍스트 데이터 파일은 다음과 같다 : 나는 csv 파일이 데이터 파일을 변환해야하거나 분석 할 파일을 엑셀변환 텍스트 데이터 파일

{1000}xxx{1200}xxx{3000}xxxxxx{5000} 
{1000}xx{1500}xxxxxx{4000}xx{6000} 
{1000}xxxx{1600}xxx{3000}xxx{6000} 
... 

. 나는 엑셀 또는 다른 소프트웨어를 개조했다. 하지만 작동하지 않습니다.

VB를 사용하여이를 수행 할 수 있습니까? 나는 오랫동안 (10 년 이상) VB를 사용하지 않았다.

죄송합니다. 나는 그것을 명확하게하지 않았다.

중괄호 안의 숫자는 필드 이름입니다. 각 레코드에는 동일한 필드가 없습니다. 후 다음과 같이해야한다 변환 결과 : (- 20 기록 10)

(header line) 1000 1200 1500 1600 3000 4000 5000 6000 
(record line) xxx xxx   xxx  xxx 
     .  xxx  xxx   xxx  xxx 
     .  xxx    xxx xxx   xxx 

우리는 텍스트 데이터 파일을 매일 있습니다. 데이터가 크지는 않지만 csv 파일로 변환 할 수있는 파일을 다시 입력 할 필요가 없습니다. 이것은 우리에게 많은 시간을 할애 할 수 있습니다.

+0

결과가 어떻게 표시됩니까? – TAS

답변

0

당신은 거의 확실히 VB와 같은 프로그래밍 언어를 사용할 수 있습니다. 나는 당신이 그렇게 할 필요가 있는지 확신하지 못합니다.

같은 유형의 파일을 계속해서 변환하는 프로그램을 작성하려는 경우 VB.net에서 프로그램을 빌드하는 것이 좋습니다.

FYI, 귀하가해야 할 일에 대해 더 많이 이해하지 않고도 조언을 제공합니다. 예를 들어, 파일의 크기, 수행 빈도, 대상 형식 등 ...

...하지만 내가 제공 한 대답은 사용자가 묻는 질문에 대답했습니다! ... 내가 담당자 포인트를 추구하고, 데이터가 어떻게 구성되어 있는지 당신의 설명에 비추어)

+0

여기에는 평판 점수가 일반적으로 문제 해결을 위해 부여됩니다. 문제가 명확하게 설명되어 있지 않은 경우 의견을 통해 OP 질문에 찾아보십시오. – Neolisk

+0

답장을 보내 주셔서 감사합니다. 우리는 매일 이것을 변환합니다. 비록 데이터 volumn이 크지는 않지만. 매일 10-20 거래가 있습니다. 중괄호 안에있는 숫자는 실제로 필드 이름입니다. 모든 레코드에는 동일한 필드가 없습니다. 우리는이 텍스트 데이터 파일을 csv로 변환하려고 시도했거나 파일을 엑셀로 보려고했습니다. – user2196273

+0

Neolisk, 나는 의견을 게시 할 수있는 능력을 얻으려고합니다. (. – Doug

0

을 : 샘플 데이터에서

Imports System.IO 
Imports System.Text 
Imports System.Text.RegularExpressions 

Module Module1 

    Class Cell 
     Property ColumnName As String 
     Property Value As String 

     ' To help with debugging/general usage 
     Public Overrides Function ToString() As String 
      Return String.Format("Col: {0} Val: {1}", ColumnName, Value) 
     End Function 
    End Class 

    Dim table As New List(Of List(Of Cell)) 

    Sub Main() 
     Dim src As String = "C:\temp\sampledata.txt" 
     Dim dest = "C:\temp\sampledata.csv" 

     Dim colNames As New List(Of String) 

     ' This regex will look for zero or more characters ".*" surrounded by braces "\{ \}" and 
     ' collect the zero or more characters in a group "()". The "?" makes it non-greedy. 
     ' The second capture group "()" gets all the characters up to but not including 
     ' the next "\{" (if it is present). 
     Dim cellSelector = New Regex("\{(.*?)\}([^\{]*)") 

     ' Read in the cells and record the column names. 
     Using inFile = New StreamReader(src) 
      While Not inFile.EndOfStream 
       Dim line = inFile.ReadLine 
       Dim rowContent As New List(Of Cell) 
       For Each m As Match In cellSelector.Matches(line) 
        rowContent.Add(New Cell With {.ColumnName = m.Groups(1).Value, .Value = m.Groups(2).Value}) 
        If Not colNames.Contains(m.Groups(1).Value) Then 
         colNames.Add(m.Groups(1).Value) 
        End If 
       Next 
       table.Add(rowContent.OrderBy(Function(c) c.ColumnName).ToList) 
      End While 
     End Using 

     colNames.Sort() 

     ' add the header row of the column names 
     Dim sb As New StringBuilder(String.Join(",", colNames) & vbCrLf) 

     ' output the data in csv format 
     For Each r In table 

      Dim col = 0 
      Dim cellNo = 0 

      While cellNo < r.Count AndAlso col < colNames.Count 
       ' If this row has a cell with the appropriate column name then 
       ' add the value to the output. 
       If r(cellNo).ColumnName = colNames(col) Then 
        sb.Append(r(cellNo).Value) 
        cellNo += 1 
       End If 

       ' add a separator if is not the last item in the row 
       If col < colNames.Count - 1 Then 
        sb.Append(","c) 
       End If 

       col += 1 

      End While 

      sb.AppendLine() 

     Next 

     File.WriteAllText(dest, sb.ToString) 

    End Sub 

End Module 

, 출력은

1000,1200,1500,1600,3000,4000,5000,6000 
xxx,xxx,,,xxxxxx,,, 
xx,,xxxxxx,,,xx,,, 
xxxx,,,xxx,xxx,,,, 

I입니다 마지막 열에는 데이터가 들어 있지 않습니다. 그저 복사 및 붙여 넣기 오류 또는 고의입니까?

편집 : 옵션을 사용합니다.에 대한 옵션이 유추됩니다. 따라서 일부 형식 선언이 누락되었습니다.

관련 문제