从不同长度和列的数据列表中创建csv

时间:2013-09-25 21:14:33

标签: csv autohotkey

我有一个旧DCS系统的数据点列表,我想迁移到一个更易于管理的组织(最好是csv)。我想将每个条目类型放入它自己的列中。最后,我还希望在给定修改后的csv文件的情况下将其转换回原始格式。这是几个条目的例子:

{SYSTEM ENTITY 95UA114( )                                                      }  
&T DIGINHG                                                                      
&N 95UA114                                                                      
UNIT     = FD  
PTDESC   ="95C-101 COMPRESSOR S/D  "  
KEYWORD  ="C101 S/D"  
PRIMMOD  = HPFD01G        
ASSOCDSP ="HPFD01~1"  
$CDETAIL ="        "  
HWYNUM   = 08  
PNTBOXTY = DHP           
BOXNUM   = 8   
PTDISCL  = FULL          
LOADDEST = HG_HIWAY      
SLOTNUM  = 12  
INPTSSLT = 10  
NMBRINPT = 1   
$AUXUNIT = --  
$REALARM = 0     
DIGALFMT = STATE2        
DLYTIME  = 0   
CHOFSTPR = NOACTION      
CNFERRPR = EMERGNCY      
OFFNRMPR = EMERGNCY      
CRITSCAN = OFF           
CCRANK   = NEITHER       
EIPPCODE = --                    
EIPEVENT = ANY           
EIPENB   = ENABLE        
ALENBST  = ENABLE        
STATE2   ="S/D_BYP "  
STATE1   ="NORMAL  "  
UBOXCLR  = RED           
LBOXCLR  = GREEN       
OVERVAL  = ON          
INPTDIR  = DIRECT      
PNTBOXIN = 1   
PNTPCTY  = MODICON       
PCADDRI1 = 2097  
SPECIFI1 = 1

不需要第一行(系统实体),因为信息是冗余的。但是,列(单位,ptdesc)并不总是相同。我打算使用autohotkey来做这件事,但如果有人有更好的建议,我会全力以赴。现在,我有代码读取文件并分离每个实体并拆分=处的每一行以确定每列的值,但将它们排成一行证明是一个挑战。我能想到处理它的唯一方法是使用2d数组,但编写起来会很麻烦,而且我确信有更好/更有效的方法(因为文件大约是21k行/ 500个实体) )。

numEntries = 0
AutoTrim, Off
outFile = test.csv
filedelete, %outfile%
filereadline, columns, columns.txt, 1
fileappend, TAG`,NAME`,%columns%`r`n, %outfile%
stringsplit, columns, columns,`,
numcolumns=%columns0%
msgbox %numcolumns%
Loop, Read, H3ALL.EB
{
    ifinstring, A_LoopReadLine,=
    {
        i++
        data%i%=%A_LoopReadLine%
        continue
    }       
    ifinstring, A_LoopReadLine,SYSTEM ENTITY
    {
        numEntries+=1
        if(numEntries > 1)
        {
            fileappend,`r`n,%outfile%
            Loop %i%
            {
                element := data%A_Index%
                stringsplit, element, element,=
                Loop %numcolumns%
                {
                    test1=%element1%
                    test2:=columns%A_Index%
                    if (test1=test2)
                    {
                        ;add to correct column
                    }
                }
            }
            data=
            i=0
        }
        continue
    }
    ifinstring, A_LoopReadLine,&T
    {
        stringsplit, line, A_LoopReadLine,%A_SPACE%
        tag=%line2%
        fileappend,%tag%`,,%outfile%
        ;msgbox the tag is %tag%
        continue
    }
    ifinstring, A_LoopReadLine,&N
    {
        stringsplit, line, A_LoopReadLIne,%A_SPACE%
        name=%line2%
        fileappend,%name%`,, %outfile%
        ;msgbox the name is %name%
        continue
    }   
}
msgbox DONE!

工作代码:

i=0
j=0
outFile = test.csv
filedelete, %outfile%
AutoTrim, off

filereadline, columns, columns.txt, 1
fileappend,%columns%`r`n,%outfile%
stringsplit, columns, columns,`,
numColumns=%columns0%

Loop, Read, H3ALL.EB
{
    ifinstring, A_LoopReadLine,=
    {
        i++
        stringsplit, line, A_LoopReadLine,=
        loop %numColumns% {
            test1:=columns%A_Index%
            test2=%line1%
            if(test1=test2) {
                dataArray%A_Index%_%j%=%line2%
                ;msgbox column %test1% (%A_Index%) contains %line2%
            }
        }
        continue
    }       
    ifinstring, A_LoopReadLine,SYSTEM ENTITY
    {
        j++
        i=0

        continue
    }
    ifinstring, A_LoopReadLine,&T
    {
        i++
        stringsplit, line, A_LoopReadLine,%A_SPACE%
        dataArray%i%_%j%=%line2%
        continue
    }
    ifinstring, A_LoopReadLine,&N
    {
        i++
        stringsplit, line, A_LoopReadLIne,%A_SPACE%
        dataArray%i%_%j%=%line2%
        continue
    }   
}
outerIndex=0
Loop %j% {
    outerIndex++
    Loop %numColumns% {
        cell:=dataArray%A_Index%_%outerIndex%
        fileappend,%cell%`,,%outfile%
    }
    fileappend,`r`n,%outfile%
}

2 个答案:

答案 0 :(得分:0)

让它运转起来:

i=0
j=0
outFile = test.csv
filedelete, %outfile%
AutoTrim, off

filereadline, columns, columns.txt, 1
fileappend,%columns%`r`n,%outfile%
stringsplit, columns, columns,`,
numColumns=%columns0%

Loop, Read, H3ALL.EB
{
    ifinstring, A_LoopReadLine,=
    {
        i++
        stringsplit, line, A_LoopReadLine,=
        loop %numColumns% {
            test1:=columns%A_Index%
            test2=%line1%
            if(test1=test2) {
                dataArray%A_Index%_%j%=%line2%
                ;msgbox column %test1% (%A_Index%) contains %line2%
            }
        }
        continue
    }       
    ifinstring, A_LoopReadLine,SYSTEM ENTITY
    {
        j++
        i=0

        continue
    }
    ifinstring, A_LoopReadLine,&T
    {
        i++
        stringsplit, line, A_LoopReadLine,%A_SPACE%
        dataArray%i%_%j%=%line2%
        continue
    }
    ifinstring, A_LoopReadLine,&N
    {
        i++
        stringsplit, line, A_LoopReadLIne,%A_SPACE%
        dataArray%i%_%j%=%line2%
        continue
    }   
}
outerIndex=0
Loop %j% {
    outerIndex++
    Loop %numColumns% {
        cell:=dataArray%A_Index%_%outerIndex%
        fileappend,%cell%`,,%outfile%
    }
    fileappend,`r`n,%outfile%
}

答案 1 :(得分:0)

这是您的替代解决方案(我在您发布自己的解决方案之前开始编写它)。我没有意识到你有一个列文件,所以我从文件中提取列标题。

outFile = test.csv
FileRead, test, H3ALL.EB
filedelete, %outfile%
columns := Object()

;get all the columns names
temp := RegExReplace(test, "`a)(=.*|{SYSTEM ENTITY .*)")
;sort and make unique
sort, temp, U

;make it an array
loop, parse, temp, `n
{   if(A_LoopField == "")
    {   continue
    }
    columns.Insert(A_loopfield)
}

;write the columns to file
for key, val in columns
{   FileAppend, % val ",", % outfile
}
FileAppend, `r`n, % outfile

;split the entries up
test := RegExReplace(test, "`a)\{SYSTEM ENTITY .*", "``")
entries := StrSplit(test , "``")

;write each entry as a row in the csv
for key, val in entries
{   if(val = "")
    {   continue
    }
    row := Object()
    loop, parse, val, `n
    {   StringSplit, data, A_LoopField, =
        row[data1] := data2
    }
    for key2, val2 in columns
    {   FileAppend, % row[val2] ",", % outfile
    }
    FileAppend, `r`n, % outfile
}