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 + "\n"
                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])

 

供参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值