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