大家好我输出有点问题我的文件带有时间戳,这是我的代码
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
。
答案 0 :(得分:4)
要生成带有时间戳的文件名,请使用time module中的strftime()
来获取必要格式的时间并将其与文件名模式连接起来:
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")
答案 1 :(得分:1)
这是你正在寻找的吗?它以您指定的格式返回包含所需时间信息的字符串:
import time
tme=time.localtime()
timeString=time.strftime("%m,%d,%y,%H:%M:%S", tme)
现在您只需要按照您想要的方式对其进行格式化并附加到文件名。有很多方法可以做到,一种非常粗略但有效的方法是:
outFileName='epg_slo_xml'+timeString+'.txt'
另一种方法是做这样的事情:
outFileName='epg_slo_xml{}.txt'.format(timeString)
但这仅适用于Python 2.7及更高版本。我还没有使用它(但是)我认为Python 3.X是相似的。