python时间输出程序_带时间戳的Python输出文件

Hi guys I have a little problem with output my file with time stamp here is my code

input_file = open('DVBSNOOP/epg_slo_sort.txt', "r") # read

output_file = open('SORT/epg_slo_xml.txt', "w") # write

for line in input_file:

line = re.sub(r"\d{8}:|\d{7}:|\d{6}:|\d{5}:|\d{4}:","", line) # remove nubers from start line example --> 10072633: etc...

line = line.replace("[= --> refers to PMT program_number]","").replace("Service_ID:","Program") # remove = --> refers to PMT program_number] and replace Service_ID to Program

line = line.replace("0xdce","").replace("Start_time:","Start") # remove 0xdce and change Start_time to start

line = line.replace("0x0","") # remove 0xdce

line = line.replace("event_name:","Title") # change to Title from event_name

line = line.replace("Program: 14 (00e)","") # remove program 14 does not exist

line = line.replace("-- Charset: ISO/IEC 8859 special table","") # remove -- Charset: ISO/IEC 8859 special table

line = line.replace("-- Charset: ISO/IEC special table","")# remove -- Charset: ISO/IEC special table

line = line.replace("[=","").replace("]","") # remove [=]

line = line.replace("Duration:","Duration").replace("0x00","").replace("0x000","").replace("0x","")

line = line.replace('"..',"").replace('"',"").replace(" . . .","").replace("-","") # remove ".."

line = re.sub(r"Start: \d{2}|\d{4}|\d{3}|\d{7}\d{9}\d{6}","",line) # remove numbers after

line = re.sub(r"Duration: \d{2}|\d{6}|\d{7}|d{5}\d{8}\d{4}|\d{3}|\d{9}","Duration",line) # remove numbers affter data

line = re.sub(r"^//","",line) # remove / /

line = re.sub(r"\([^)]*\)","",line) # remove brackets

line = re.sub(r"Program 14","",line) # remove program 14

output_file.write(line) # write to file

I want my output like epg_slo_sort(M;D;Y:Time).txt.

解决方案

To generate a filename with a timestamp use strftime() from the time module to get the time in the necessary format and concatenate it with your file name pattern:

import time

current_time = time.strftime("%m.%d.%y %H:%M", time.localtime())

output_name = 'SORT/epg_slo_xml%s.txt' % current_time

output_file = open(output_name, "w")

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值