我需要一些关于代码的帮助:
#!/usr/bin/python
output = open('XML/epg.xml','w')
with open('SORT/epg_slo_xml.txt','r') as txt:
for line in txt:
var1 = 'Program'
if 'Program' in line:
if var1 in line !=True:
var = line
var_s = '<program>'
var_stop = '</program>'
var_uk = var_s+line+var_stop
if 'Start' in line:
var2=line
var_s1='<start>'
var_stop1 ='</start>'
var_uk1 = var_s1+var2+var_stop1
var_out = var_uk+var_uk1
if 'Duration' in line:
var3=line
var_s2='<duration>'
var_stop2 ='</duration>'
var_uk2 =var_s2+var3+var_stop2
var_out2= var_uk+var_uk1+var_uk2
if 'Title' in line:
var4=line
var_s3='<title>'
var_stop3 ='</title>'
var_uk3 =var_s3+var4+var_stop3
var_out3 =var_uk+var_uk1+var_uk2+var_uk3
output.write('<item>'+var_out3+'</item>')
如何打破循环,以及何时写入输出所有 var_uk 语句?你能给我一些建议吗?我尝试了很多东西,但没有结果。
我的文件看起来像:
Program 10
Start 20130918 11:50:00
Duration 02:10:00
Title Obvestila
Program 5
Start 20130918 12:00:00
Duration 00:20:00
Title TV TRANSFRONTALIERA TG R
Program 10
Start 20130918 11:50:00
Duration 02:10:00
Title Obvestila
Program 13
Program 13
Start 20130918 12:00:00
Duration 00:15:00
Title TV prodaja
Start 20130918 12:15:00
Duration 01:00:00
Title S hrano do vitkosti
Start 20130918 13:15:00
Duration 00:25:00
Title Najini začetki
Start 20130918 13:40:00
Duration 00:30:00
Title Oddamo v najem
Start 20130918 14:10:00
Duration 01:00:00
Title Colin in Justin prenavljata domove
我知道为每个 if 语句逐行编写输出,但我怎样才能写出所有语句?