0

我有一个旧 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

不需要第一行(系统实体),因为信息是多余的。然而,列 (Unit, ptdesc) 并不总是相同的。我打算使用自动热键来做到这一点,但如果有人有更好的建议,我会全力以赴。现在,我有读取文件并分隔每个实体并在 处拆分每一行=以确定每一列的值的代码,但将它们排列起来是一个挑战。我能想到的唯一处理方法是使用 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%
}
4

2 回答 2

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
}
于 2013-09-26T14:52:45.287 回答
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%
}
于 2013-09-26T13:16:29.687 回答