我需要读取AFP文件并提取它的TLE,如账号、客户端全名。。。AFP文件格式良好,结构非常清晰。我不想添加任何外部库。
AFP是一种简单的格式,它由结构化字段组成,您的第一步是解码它们,下载它:"混合对象文档内容体系结构参考";阅读前50页并编写代码,将afp拆分为结构化字段,以便创建一个简单的文件转储。
出色的
它是用Python编写的,我很久以前就写过了,从那以后就没有碰过它,但它起了作用。我想补充一些评论,但我什么都记不起来了。可能有一些用处。
import datetime
start = datetime.datetime.now()
statementlist=[]
ptxlist = []
with open('C:\temp\AFP\bigfile', 'rb') as fs:
bngcmd = b'xA8xAD'
engcmd = b'xA9xAD'
ptxcmd = b'xEEx9B'
tlecmd = b'xA0x90'
inDoc = False
while True:
# 'Read SFI header info
byte = fs.read(6)
if not byte:
break
# 'get command info
SFICommand = byte[4:6]
SFISize = (byte[1]<<8) + byte[2]
if not inDoc and SFICommand == bngcmd:
inDoc = True
ptx = bytearray()
fs.read(SFISize-5)
elif inDoc and SFICommand == engcmd:
inDoc = False
ptxlist.append(ptx)
currentsize = 0
fs.read(SFISize-5)
elif inDoc and SFICommand == ptxcmd:
fs.read(3)
ptx.extend(bytearray(fs.read(SFISize-8)))
else:
fs.read(SFISize-5)
inptxcmd = b'x2BxD3'
for ptx in ptxlist:
i = 0
ptxlength =len(ptx)
utfstring=''
while i < ptxlength:
if ptx[i:i+2] == inptxcmd:
i+=2
ptxsize = ptx[i]
ptxcmd = ptx[i+1]
if ptxcmd == 0xDA or ptxcmd == 0xDB:
utfstring += ptx[i+2:i+ptxsize].decode("EBCDIC-CP-BE").strip() + 'u0009'
i+=ptxsize
statementlist.append(utfstring + 'n')
with open('C:\temp\AFP\pythonout.txt', 'w',encoding='ASCII') as outfile:
for statement in statementlist:
outfile.write(statement)