Python以二进制形式读取数据

时间:2015-06-03 16:38:46

标签: python

我想在logData中读取二进制文件,然后在第二个for循环中解析二进制输出,就像对于字符串一样,但是对于二进制文件。这可能吗?

logData = open(sys.argv[1]).readlines()
processedSources = sys.stdin.readlines()

stringDictionary = {}
for line in processedSources:
    # Match data looking for MODULE_ID, LOG_LINE, ARG_COUNT, FILE_NAME, DATA_STRING
    match = re.search("(\d+),\s+(\d+),\s+(\d+),\s+(.*),\s+(\".*\")", line)
    if match:
        moduleId = int(match.group(1))
        logLine = int(match.group(2))
        argCount = int(match.group(3))
        fileName = match.group(4)
        outputString = match.group(5)    
        stringDictionary[(moduleId, logLine)] = [ moduleId, logLine, argCount, fileName, outputString ]
    else:
        print "Failed string dictionary on: " + line

for line in logData:
    # Match data looking for MODULE_ID, LOG_LINE, ARG_COUNT, ARGUMENTS
    matchLogData = re.split("\s+", line)
    if matchLogData:
        moduleId = int(matchLogData[0], 16)
        logLine = int(matchLogData[1], 16)
        argCount = int(matchLogData[2], 16)
        if stringDictionary[(moduleId, logLine)]:
            processedData = stringDictionary[(moduleId, logLine)]
            if argCount != processedData[2]:
                print "Argument count mismatch on : " + line
                print "   expected %d found %d" % (argCount, processedData[2])
            else:
                index = 0
                logString = "%02x:%4d:%s:" + processedData[4]
                logData = (processedData[0], processedData[1], processedData[3])
                while index < argCount:
                    logData = logData + (int(matchLogData[index+3], 16),)
                    index = index + 1
                print logString % logData
        else:
            print "ModuleId:%d Line:%d, not found in source dictionary" % (moduleId, logLine)
            print "   Line data: " + line
    else:
        print "Expected log input data mismatch MODULE_ID LOG_LINE ARG_COUNT ARGS"
        print "Line: " + line

0 个答案:

没有答案