Python re.search from text file
Hi guys I have problem with re.search from text matching text names and
print them to new file as output xml.
My python code looks like
/usr/bin/env python import re
output = open('epg.xml','w') n = 0 print >> output, ''+'\t' print >>
output, ''
with open('epg_slo_utf_xml.txt','r') as txt: for line in txt: if
re.search('Program', line) !=None: n = n + 1 e =''+line+''
if re.search('Start', line) !=None:
n = n + 1
f ='<start>'+line+'</start>'
if re.search('duration', line) !=None:
n = n + 1
g ='<duration>'+line+'<duration>'
wo = e + f + g
print >> output, wo
print >> output , '`
And my output will be like xml :
"{ Program 9 Start 2013-09-12 06:45:00 Program 3
Start 2013-09-12 07:10:00 }
But problem is with two more tags ( and ), what i get when i run script is
Traceback (most recent call last): File "./epg_transform.py", line 25, in
<module> wo = e + f + g NameError: name 'g' is not defined
Example of my text file is : `Program 6
Start 2013-09-12 06:40:00 Duration 00:50:00 Title Vihar
Program 9
Start 2013-09-12 06:45:00 Duration 00:29:00 Title TV prodaja
Program 7
Program 6
Program 13
Start 2013-09-12 06:20:00
Duration 00:50:00
Title Kursadije `
I don't know is there problem with re.search where he don't find program +
all other (start, duration, title). Can you help me with this problem? BR!
No comments:
Post a Comment