• Python以不可见字符作为列分割符


    # -*- coding: utf-8 -*-
    
    import sys
    import time
    
    CTRL_A='x01'
    CTRL_B='x02'
    
    thedate = ''
    thetime = ''
    sn = ''
    asr = ''
    intent = ''
    nlp = ''
    domain = ''
    
    
    def speechHandle(inputFile,outputFile):
        print "********** speechHandle " + inputFile + " start **********"
        try:
            input = open(inputFile, 'r')
            output = open(outputFile, 'a+')
            while True:
                line = input.readline()
                if len(line.strip()) < 1:
                    break
                fileds = line.split(CTRL_A)
                fieldsLen = len(fileds)
                if fieldsLen == 6:
                    for fIndex in range(0,6):
                        kv = fileds[fIndex].split(CTRL_B)
                        kvLen = len(kv)
                        if kvLen == 2:
                            if kv[0].strip()=='time':
                                unixtimestamp = kv[1]
                                timetmp = time.localtime(int(unixtimestamp)/1000)
                                thedate = time.strftime("%Y-%m-%d", timetmp)
                                thetime = time.strftime("%H:%M:%S",  timetmp)
                            elif kv[0].strip()=='id':
                                sn = kv[1]
                            elif kv[0].strip()=='asr':
                                asr = kv[1]
                            elif kv[0].strip() == 'intent':
                                intent = kv[1].strip()
                            elif kv[0].strip() == 'domain':
                                domain = kv[1]
                    lineOut = thedate + CTRL_A + thetime + CTRL_A + sn + CTRL_A + asr + CTRL_A + intent + CTRL_A + domain + "
    "
                    output.write(lineOut)
                else:
                    print line
        except IOError as ioerr:
            print 'File Error' + str(ioerr)
            exit(-1)
        finally:
            input.close()
            output.close()
    
        print "********** speechHandle " + inputFile + " end **********"
    
    
    if __name__ == '__main__':
        if len(sys.argv) != 3:
            print "Please input inputFile and outputFile"
        else:
            speechHandle(sys.argv[1], sys.argv[2])

    供参考

  • 相关阅读:
    Sort函数的用法
    hdu 1087 Super Jumping! Jumping! Jumping!(最大上升子序列和)
    hdu 1159 Common Subsequence(LCS)
    最长公共子序列
    蓝桥杯 -- 取字母
    蓝桥杯 -- 串的处理
    蓝桥杯 -- 奇怪的比赛
    蓝桥杯 -- 微生物繁殖
    hdu 1159 Common Subsequence(最长公共子序列)
    hdu 2458 Kindergarten
  • 原文地址:https://www.cnblogs.com/zhzhang/p/6840123.html
Copyright © 2020-2023  润新知