带时间戳的Python输出文件 [英] Python output file with timestamp
本文介绍了带时间戳的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
我想要我的输出,如 epg_slo_sort(M; D; Y:Time).txt
。
I want my output like epg_slo_sort(M;D;Y:Time).txt
.
推荐答案
要生成带有时间戳的文件名,请使用 strftime()
时间模块以获取必要格式的时间并将其与文件连接名称模式:
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")
这篇关于带时间戳的Python输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文