URN Logo
UNIX Resources » Linux » China Linux Forum » Python ±à ³Ì » µÚ8Ò³ » Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
announcement ÉùÃ÷: ±¾Ò³ÄÚÈÝΪÖйúLinuxÂÛ̳µÄÄÚÈݾµÏñ£¬ÎÄÕµİæÈ¨ÒÔ¼°ÆäËûËùÓеÄÏà¹ØÈ¨ÀûÊôÓÚÖйúLinuxÂÛ̳ºÍÏàÓ¦ÎÄÕµÄ×÷Õߣ¬Èç¹û×ªÔØ£¬Çë×¢Ã÷ÎÄÕÂÀ´Ô´¼°Ïà¹Ø°æÈ¨ÐÅÏ¢¡£
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  Òµ½çÐÂÎÅÓëÆÀÂÛ
  ×ÔÓÉÈí¼þÔÓ̸
  IT ÈËÉú
  LinuxÈí¼þ¿ìµÝ
  ·­Òë×÷·»
  LinuxͼÊéÓëÆÀÂÛ
  GNU Emacs/XEmacs
  Linux ÖÐÎÄ»·¾³ºÍÖÐÎÄ»¯
  Linux×ÀÃæÓë°ì¹«Èí¼þ
  Linux ¶àýÌåÓëÓéÀÖ°æ
  ×ÔÓÉÖ®´°Mozilla
  ±Ê¼Ç±¾µçÄÔÉϵÄLinux
  Gentoo
  Debian Ò»×å
  ÍøÂç¹ÜÀí¼¼Êõ
  Linux °²×°ÓëÈëÃÅ
  WEB·þÎñÆ÷ºÍFTP·þÎñÆ÷
  ÓòÃû·þÎñÆ÷ºÍÓʼþ·þÎñÆ÷
  Linux·À»ðǽºÍ´úÀí·þÎñÆ÷Ó¦ÓÃ
  Îļþ¼°´òÓ¡·þÎñÆ÷
  ¼¼ÊõÅàѵÓëÈÏÖ¤
  LinuxÄں˼¼Êõ
  Linux ǶÈë¼¼Êõ
  LinuxÉ豸Çý¶¯³ÌÐò
  Linux ¼¯Èº¼¼Êõ
  LINUXƽ̨Êý¾Ý¿â
  ÏµÍ³ºÍÍøÂ簲ȫ
  CPU Óë ±àÒëÆ÷
  ÏµÍ³¼ÆËãÑо¿ËùרÀ¸
  LinuxϵÄGUIÈí¼þ¿ª·¢
  C/C++±à³Ì°æ
  PHP ¼¼ Êõ
  Java&jsp¼¼Êõ
  Shell±à³Ì¼¼Êõ
  Perl ±à ³Ì
  Python ±à ³Ì
  XML/Web Service ¼¼Êõ
  ÓÀÔ¶µÄUnix
  FreeBSDÊÀ½ç
   
Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
 
 
 
 
 
ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - zhyeye [2005-07-25 17:21 | 855 byte(s)]
 
Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - zhyeye [2005-07-25 23:22 | 1,470 byte(s)]
 
Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - zhyeye [2005-07-26 16:18 | 40 byte(s)]
 
Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - limodou [2005-07-26 16:30 | 2,240 byte(s)]
 
Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - zhyeye [2005-07-26 19:32 | 387 byte(s)]
 
limodou´ó¸ç£¬³ÌÐòÓеãÎÊÌâ - zhyeye [2005-07-26 17:58 | 1,110 byte(s)]
 
limodou´ó¸ç£¬ÔÙ´ÎÇëÇó°ïÖú£¡ - zhyeye [2005-07-27 00:23 | 2,562 byte(s)]
 
 
 
 
 
 
 
Ì«¸ÐлÄúÁË£¡ - zhyeye [2005-07-27 21:04 | 181 byte(s)]
 
Re: Ì«¸ÐлÄúÁË£¡ - limodou [2005-07-27 23:09 | 266 byte(s)]
 
¹ØÓÚÄ£°å - alula [2005-07-28 12:00 | 7,245 byte(s)]
 
Re: ¹ØÓÚÄ£°å - alula [2005-07-28 12:10 | 5,246 byte(s)]
 
Re: ¹ØÓÚÄ£°å - limodou [2005-07-28 14:27 | 3,338 byte(s)]
 
Re: ¹ØÓÚÄ£°å - alula [2005-07-29 09:48 | 211 byte(s)]
 
Re: ¹ØÓÚÄ£°å - limodou [2005-07-29 10:41 | 470 byte(s)]
 
 
 
Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - zhyeye [2005-07-26 16:49 | 45 byte(s)]
 
Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - limodou [2005-07-25 20:54 | 90 byte(s)]
 
Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡ - passworld [2005-07-25 19:09 | 628 byte(s)]
 
 
 
 
 
Subject: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: zhyeye    Posted: 2005-07-21 21:44    Length: 2,510 byte(s)
[Original] [Print] [Top]
ÎÒÊÇÐÂÊÖ£¬ÏÖÔÚÓÐÒ»¸öÎı¾Îļþ£¬ÏëÒª°ÑÀï±ßµÄÊý¾Ýת»¯³ÉXMLÎļþ¸ñʽ£¬¿ÉÊÇÔõô¶¼Åª²»ºÃ£¬Çëǰ±²°ï棬¸Ð¼¤²»¾¡£¡£¡
Îı¾ÎļþÈçÏ£»(123.txt)
xiaoyu female 20 english 80 maths 88 chinese 92 song 44 yang 45 beijing
dawei male 21 english 85 maths 94 chinese 89 he 46 zhang 48 shanghai
......
......
ÏëÒªÉú³ÉÈçÏÂxmlÎļþ£»
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?>
<xueshengzhuangtai>
<number1>
<name>xiaoyu</name>
<gender>female</gender>
<age>20</age>
<result_exam>
<course>english</course>
<score>80</score>
<course>maths</course>
<score>88</score>
<course>chinese</course>
<score>92</score>
</result_exam>
<family>
<mother>song</mother>
<age>44</age>
<father>yang</father>
<age>45</age>
</family>
<city>beijing</city>
</number1>
.
.
.
.
</xueshengzhuangtai>
ÎÒÊÔÁ˺öà´Î£¬µ«ÊǶ¼Ö»ÄÜÉú³ÉÒ»¼¶½á¹¹£¬¾ÍÊÇÕâÑùµÄ£»
<number1>
<name>xiaoyu</name>
<gender>female</gender>
<age>20</age>
<result_exam>
english 80 maths 88 chinese 92
</result_exam>
<family>song 44 yang 45</family>
<city>beijing</city>
</number1>

ÇëÇó°ï棬¶àл¶àл£¡
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: limodou    Posted: 2005-07-21 22:34    Length: 18 byte(s)
[Original] [Print] [Top]
³ÌÐòÌù³öÀ´¿´Ò»¿´¡£
----
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: zhyeye    Posted: 2005-07-21 23:12    Length: 3,992 byte(s)
[Original] [Print] [Top]
ÎÒ°ÑÔ­Îļþ¸Ä³ÉÕâÑùÁË£¬£¨ÎªÁËÇø·Ö×ֶΣ©
xiaoyu female 20 english,80 maths,88 chinese,92 song;44;yang;45
beijing
dawei male 21 english,85 maths,94 chinese,89 he;46 zhang;48
shanghai

³ÌÐòÈçÏ£º

import sys
print "Content-type:text/xml "

print """<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?>"""

#open data file
try:
file = open( "xszt.txt","r" )
except IOError:
sys.exit( "Error opening file" )

print "<xueshengzhuangtai>" #write root element

#list of tuples:(special character,entity reference)
replaceList = [ ( "&", "&amp;" ),
( "<", "&lt;" ),
( ">", "&gt;" ),
( '"', "&quot;" ),
( "'", "&apos;" ) ]

#replace special characters with entity reference
for currentLine in file.readlines():

for oldValue, newValue in replaceList:
currentLine = currentLine.replace( oldValue, newValue )

#extract element
name, gender, age, result_exam, family, city = currentLine.split( " " )
city = city.strip() #remove carriage return

#write xueshengzhuangtai element
print """ <xueshengzhuangtai>
<Name>%s</Name>
<gender>%s</gender>
<age>%s</age>
<result_exam>%s</result_exam>
<family>%s</family>
<city>%s</city>
</xueshengzhuangtai>""" % ( name, gender, age, result_exam, family, city )

for result_exam in file.readlines():

for oldValue, newValue in replaceList:
currentLine = currentLine.replace( oldValue, newValue )

course, score = currentLine.split( ", " )
score = score.strip() #remove carriage return

#write result_exam element
print """ <result_exam>
<course>%s</course>
<score >%s</score >
</result_exam>""" % ( course, score )

for family in file.readlines():

for oldValue, newValue in replaceList:
currentLine = currentLine.replace( oldValue, newValue )

mother, age, father, age = currentLine.split( ";" )
age = age.strip() #remove carriage return

#write family element
print """ <family>
<mother>%s</mother>
<age>%s</age>
<father>%s</father>
<age>%s</age>
</family>""" % ( mother, age, father, age )

file.close()

print "</contacts>"
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: zhyeye    Posted: 2005-07-21 23:20    Length: 63 byte(s)
[Original] [Print] [Top]
ÎÒ¸Õ¿ªÊ¼Ñ§£¬»¹²»¶®µÃÔõôÓã¬Ï£Íûlimodou´ó¸ç¸ç²»Áߴͽ̣¬¶àлÁË£¡
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: limodou    Posted: 2005-07-22 09:05    Length: 467 byte(s)
[Original] [Print] [Top]
file.readlines() Ôõô»áÔÚÑ­»·Öжà´Î³öÏÖ£¿
ת»» <, > Ö®ÀàµÄ×Ö·û¿ÉÒÔʹÓÃcgi.escape()·½·¨
½âÎöÎı¾Îļþ£¬ÓëÉú³É½á¹û²»ÊǺÜÇåÎú£¬½¨ÒéÏȽ«Îı¾µÄ½âÎö¶ÀÁ¢³öÀ´£¬È»ºó½«½âÎö³öÀ´µÄ½á¹ûÔÙ¼Ó¹¤³Éxml¸ñʽ¡£Éú³ÉxmlµÄ·½Ê½¾Í·Ç³£¶àÁË£º

1.ʹÓÃxml°ü£¬ÀûÓÃdom¶ÔÏó
2.ʹÓÃÄ£°å
3.×ÔÒÑд¼òµ¥µÄÌæ»»¸ñʽ£¬È磺

print "%(name)s %(age)d" % {'name':'test', 'age':14}

ÔÙ¸ÄÒ»¸Ä
----
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: zhyeye    Posted: 2005-07-22 16:29    Length: 202 byte(s)
[Original] [Print] [Top]
²»ºÃÒâ˼£¬limdou´ó¸ç£¬ÎÒ»¹ÊDz»Ì«Ã÷°×¸ÃÔõôȥ×ö£¬ÊÇ˵ÕâÀï²»ÄÜÓÀÔª×éÂð£¿ÎÒ¾ÍÖ»»áÒ»µãpython£¬µ«ÊÇÀϰåÒªÎÒÍê³ÉÕâ¸öÈÎÎñ£¬ËùÒԿɲ»¿ÉÒÔÇëÄú°ïæ¸Äһϣ¬»òÕßÄú¸øÎÒÍÆ¼öÒ»ÏÂʲôµØ·½ÓÐÀàËÆµÄÀý×Ó¡£
¶àлÁË£¡
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: limodou    Posted: 2005-07-22 17:39    Length: 125 byte(s)
[Original] [Print] [Top]
Èç¹ûÊÇΪÁËÍê³ÉÈÎÎñ¾ÍʹÓÃÄãÔ­À´µÄ·½·¨¾Í¿ÉÒÔ£¬µ«ÄãµÄÂß¼­ÉÏÓÐÎÊÌ⻹ÊÇÐèÒª¸ÄÒ»¸Ä£¬ÕýÈçÎÒ˵µÄ£¬ÄãÓжà¸öreadlines()²»ÖªµÀÊÇΪʲô£¿
----
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: zhyeye    Posted: 2005-07-22 18:11    Length: 959 byte(s)
[Original] [Print] [Top]
ÎÒÓöà¸öreadlines()µÄ±¾ÒâÊÇÏëÒª¶Á³ö¶þ¼¶µÄÊý¾Ý£¬Éú³É£º
<result_exam>
<course>english</course>
<score>80</score>
<course>maths</course>
<score>88</score>
<course>chinese</course>
<score>92</score>
</result_exam>
<family>
<mother>song</mother>
<age>44</age>
<father>yang</father>
<age>45</age>
</family>
µ«ÊÇÏÂÃæÁ½¸öÖØ¸´µÄreadlines()Õⲿ·Ö´úÂë¸ù±¾¾Í²»ÔËÐУ¬ÎÒÒ²²»ÖªµÀ¸ÃÔõô¸Ä£¬ËùÒÔÇëÇóÄú°ïæ¡£
¶àл£¡
[Original] [Print] [Top]
Subject: Re: Çë½Ìǰ±²ÃÇ£¬ÈçºÎʹÓÃpython½«Îı¾Îļþת»¯ÎªXMLÎļþ£¿£¿£¿
Author: limodou    Posted: 2005-07-22 21:30    Length: 2,212 byte(s)
[Original] [Print] [Top]
ÎÒ°´ÕÕÄãµÄµÚÒ»¸öÌù×ÓдÁËÒ»¸ö³ÌÐò£¬
ÒòΪÄǸö´¦ÀíÆðÀ´¸ü¼òµ¥

header = """<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?>
<xueshengzhuangtai>"""

body="""<number1>
<name>%(name)s</name>
<gender>%(sex)s</gender>
<age>%(age)s</age>
<result_exam>
<course>%(en_course)s</course>
<score>%(en_score)s</score>
<course>%(math_course)s</course>
<score>%(math_score)s</score>
<course>%(ch_course)s</course>
<score>%(ch_score)s</score>
</result_exam>
<family>
<mother>%(mother)s</mother>
<age>%(mother_age)s</age>
<father>%(father)s</father>
<age>%(father_age)s</age>
</family>
<city>%(liveing)s</city>
</number1>"""

footer="""</xueshengzhuangtai>"""

def deal_body():
for line in file('d:/a.txt'):
(name, sex, age, en_course, en_score, math_course, math_score,
ch_course, ch_score, mother, mother_age, father, father_age, liveing) =
line.split()
print body % locals()

if __name__ == '__main__':
print header
deal_body()
print footer

Ϊʲô°´µÚÒ»¸öÌù×Ó´¦Àí¸ü¼òµ¥ÄØ£¿ÒòΪ¶ÔÓÚxml±¨ÎÄÀ´ËµÊÇ·Ö²ãµÄ£¬µ«¶ÔÓÚÉú³É½á¹ûÈ´ÊDz»ÐèÒª¿
¼Âǵģ¬Ö»Òª°ÑÏàÓ¦µÄxmlÎı¾¿´³ÉÄ£¿é£¬½øÐÐÒ»Î¬Ìæ»»¼´¿É¡£
----
[Original] [Print] [Top]
Subject: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: zhyeye    Posted: 2005-07-25 17:21    Length: 855 byte(s)
[Original] [Print] [Top]
¶àлlimodou´ó¸ç£¬°ïÎÒ½â¾öÁËÒ»¸öÄÑÌ⣬ֻÊÇÈç¹ûÎĵµÖеĿ¼ÊÔ¿ÆÄ¿ºÍ¿¼ÊԳɼ¨Ò»ÏîÈç¹û²»¹Ì¶¨µÄ»°Õâ¸ö³ÌÐòºÃÏñ¾Í²»ÊÊÓÃÁË£¬±ÈÈç;
xiaoyu female 20 english 80 maths 88 chinese 92 song 44 yang 45 beijing (¿¼ÁË3ÃÅ¿Î)
xiaoming male 20 english 82 maths 84 huang 44 li 45 beijing (¿¼ÁË2ÃÅ¿Î)
dawei male 21 english 85 maths 94 chinese 89 he 46 zhang 48 shanghai (¿¼ÁË3ÃÅ¿Î)
xiaoxiao male 20 geography 87 lv 44 wang 45 beijing (¿¼ÁË1ÃÅ¿Î)
xiaohong female 21 english 88 maths 84 chinese 94 biology 86 history 78 hu 46 han 48 shanghai (¿¼ÁË5ÃÅ¿Î)
feifei male 21 hu 46 han 48 shanghai (ûÓп¼ÊԳɼ¨)
Èç¹ûÕâÑùµÄ»°¸ÃÔõô½â¾ö£¿limodou´ó¸ç¸ç£¬ÎÒÕæµÄºÜ²»ºÃÒâ˼£¬Ò»¶øÔÙÔÙ¶øÈýµÄÎÊÄú£¬ÎÒµÚÒ»´ÎûÓÐ×Ðϸ¿´ÍêÄǸötxtÎĵµ£¬Ã»ÓÐ×¢Òâµ½ºóÃæºóÃæ¿¼ÊԳɼ¨ºÍ¿¼ÊÔ¿ÆÄ¿µÄ±ä»¯£¬¸øÄúÌíÂé·³ÁË£¬Ï£ÍûÄúÄÜÔÙ°ï°ï棡¶àлÁË£¡
[Original] [Print] [Top]
Subject: Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: passworld    Posted: 2005-07-25 19:09    Length: 628 byte(s)
[Original] [Print] [Top]
ÄãÕâÑù¿ÆÄ¿»ìÅÅÊÇͦÄѶÁµÃ£¬×Ô¼º¸ø×Ô¼º³öÄÑÌâ¡£»¹ÊǰÑËùÓпÆÄ¿Ó÷ֺŷֿª£¬ÆäËûµÄÓÿոñ·Ö¿ª
±È½ÏÈÝÒס£


#!/usr/bin/python

line = "xiaoyu female 20 english 80 maths 88 chinese 92 song 44 yang 45 beijing"

def parse_line(line):
items = line.split()
rawgrade = items[3:-5]
grade = dict(zip(rawgrade[::2], rawgrade[1::2]))
return items[:3], items[-5:], grade

items = parse_line(line)
print items
# vim:ts=8:sw=4:expandtab

----
[Original] [Print] [Top]
Subject: Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: limodou    Posted: 2005-07-25 20:54    Length: 90 byte(s)
[Original] [Print] [Top]
Èç¹ûÕâÑùµÄ»°ÄãµÄÄ£°å¾ÍÓÐÎÊÌâÁË¡£ÒòΪ×îºóÃæµÄ¸¸Ä¸ÐÅÏ¢Ôõô²»¼ûÁË¡£ÄãµÄԭʼÊý¾ÝÊÇʲôÑùµÄ°¡¡£
----
[Original] [Print] [Top]
Subject: Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: zhyeye    Posted: 2005-07-25 23:22    Length: 1,470 byte(s)
[Original] [Print] [Top]
Ô­ÎļþÌ«³¤ÁË£¬ÎÒ¾ÍÁоÙÁËһС²¿·Ö£»
Xiaoyu * female * 20 * english 80 maths 88 chinese 92 * song 44 yang 45 * Beijing * (¿¼ÁË3ÃÅ¿Î)
xiaoming * male * 20 * english 82 maths 84 * huang 44 li 45 * Beijing * (¿¼ÁË2ÃÅ¿Î)
dawei male * 21 * english 85 maths 94 chinese 89 * he 46 zhang 48 * shanghai * (¿¼ÁË3ÃÅ¿Î)
xiaoxiao * male * 20 * geography 87 * lv 44 wang 45 * Beijing * (¿¼ÁË1ÃÅ¿Î)
xiaohong * female * 21 * english 88 maths 84 chinese 94 biology 86 history 78 * hu 46 han 48 * shanghai * (¿¼ÁË5ÃÅ¿Î)
feifei * male * 21 * * hu 46 han 48 *shanghai * (ûÓп¼ÊԳɼ¨)
xiaoying * female * 20 * english 80 maths 86 geography 90 * li 48 *shanghai * (¿¼ÁË3ÃÅ¿Î)
¡­¡­¡­¡­¡­
Ô­ÎļþÖÐʹÓà * À´Çø·ÖÒ»¼¶×ֶΣ¬<name>£¬<gender>£¬<age>£¬ <result_exam>£¬<family>ºÍ<city>¡£Èç¹ûijһ×Ö¶ÎΪ¿Õ£¬¾ÍÊÇÕâÑù * * ¡£
<result_exam>ºÍ<family>ÏÂÓжþ¼¶×ֶΣ¬¶þ¼¶×Ö¶ÎÒÔ¿Õ¸ñÀ´Çø·Ö£¬¶øÇÒ<result_exam>ÖÐѧÉú²Î¼Ó¿¼ÊԵĿÆÄ¿²»È·¶¨£¬ÓеIJμӵĶ࣬ÓеIJμӵÄÉÙ£¬Óеĸù±¾Ã»Óвμӣ»<family>ÖÐÓеÄÊǸ¸Ä¸Á½¸öÈ˵ÄÐÅÏ¢£¬ÓеÄÖ»ÓÐÒ»¸öÈ˵ÄÐÅÏ¢(ËùÒԾͲ»Çø·ÖÊǸ¸Ç×»¹ÊÇĸÇ×£¬Ö±½ÓÓÃ<parents>ºÍ<age>À´±íʾ)¡£

ÎÒ±¾À´ÏëŪµÃ¼òµ¥Ò»µã£¬ËùÒÔ¾ÍÓÃÁÐÁËÒÔǰµÄÀý×Ó£¬Ã»Ï뵽ȴԽŪԽÂé·³£¬ÕæµÄÊÇÌ«Âé·³ÄúÁË£¬limodou´ó¸ç¡£
Âé·³ÄúÔÙ°ïæһÏ£¬²»Ê¤¸Ð¼¤£¡
[Original] [Print] [Top]
Subject: Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: zhyeye    Posted: 2005-07-26 16:18    Length: 40 byte(s)
[Original] [Print] [Top]
²»ºÃÒâ˼£¬ÇëÄú°ï°ï棡
¶àлÁË£¡
[Original] [Print] [Top]
Subject: Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: limodou    Posted: 2005-07-26 16:30    Length: 2,240 byte(s)
[Original] [Print] [Top]
²»¹ýÎÒ²»»á°ÑÈ«²¿³ÌÐò¸øÄãдÍêµÄ£¬ÒòΪÈç¹ûÕâÑùÓÀԶҲѧ²»
»ápython¡£
ÏÂÃæÊÇÎÒдµÄ¶ÔÓÚÄãµÄÊý¾ÝµÄÒ»¸ö½âÎö³ÌÐò£º

for line in file('a.txt'):
v = [x.strip() for x in line.rstrip().split('*')][:-1]
name, sex, age, score, family, city = tuple(v)
if score:
s = score.split()
score = [(s[i], s[i+1]) for i in range(len(s)/2)]
else:
score = []
if family:
s = family.split()
family = [(s[i*2], s[i*2+1]) for i in range(len(s)/2)]
else:
family = []
print name, sex, age, score, family, city


ËüÊ×ÏȰ´*ºÅ½øÐзֽ⡣µ«ÒòΪÄãµÄÊý¾ÝÿÐÐ×îºóÓÐÒ»¸ö*ºÅ£¬
ÕâÑù·Ö½â³öÀ´ºó»á¶àÒ»¸ö¿Õ´®£¬Òò´Ë[:-1]¾ÍÊǽ«¿Õ´®È¥µô¡£
ÕâÑù£¬·Ö½âÍê±ÏºóscoreºÍfamily¿ÉÄÜΪ¶àÌõÐÅÏ¢£¬¸öÊý²»Ò»
Ñù¡£Òò´ËÏÂÃæÒ»¸öÊǶÔscoreµÄ´¦Àí£¬Ò»¸öÊǶÔfamilyµÄ´¦Àí¡£
¶ÔÓÚscoreÊǽ«ÐÅÏ¢°´¿Õ¸ñ²ð·Ö£¬È»ºóÁ½Á½×éºÏ³ÉÒ»¸ötuple¡£
×îºó·µ»ØÎªÒ»¸ölist,Èç¹ûÊý¾ÝΪ¿ÕÔòΪһ¸ö¿Õlist¡£familyÒ²ÊÇ
Èç´Ë¡£

¸ù¾ÝÄãµÄÊý¾ÝµÃµ½ÏÂÃæµÄÊä³ö½á¹û£º

Xiaoyu female 20 [('english', '80'), ('80', 'maths'), ('maths', '88')] [('song',
'44'), ('yang', '45')] Beijing
xiaoming male 20 [('english', '82'), ('82', 'maths')] [('huang', '44'), ('li', '45')]
Beijing
dawei male 21 [('english', '85'), ('85', 'maths'), ('maths', '94')] [('he', '46'),
('zhang', '48')] shanghai
xiaoxiao male 20 [('geography', '87')] [('lv', '44'), ('wang', '45')] Beijing
xiaohong female 21 [('english', '88'), ('88', 'maths'), ('maths', '84'), ('84',
'chinese'), ('chinese', '94')] [('hu', '46'), ('han', '48')] shanghai
feifei male 21 [] [('hu', '46'), ('han', '48')] shanghai
xiaoying female 20 [('english', '80'), ('80', 'maths'), ('maths', '86')] [('li',
'48')] shanghai

Òò´Ë¶àÏîµÄΪһ¸ölist£¬µ¥ÏîµÄ¾ÍÊÇÒ»¸ö×Ö·û¡£ÕâÑùÄã½á
ºÏÕâÑùµÄ½á¹ûÔÙ¼ÓÉÏÎÒÒÔǰÌù³öµÄ³ÌÐò×ÔÒѺϲ¢Ò»¸öÉú
³É×îÖÕ³ÌÐò°É¡£

BTW:ÄãµÄÊý¾ÝµÚÈýÐÐÓÐÎÊÌ⣺daweiÓëmaleÖ®¼äûÓÐÐǺš£
----
[Original] [Print] [Top]
Subject: Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: zhyeye    Posted: 2005-07-26 16:49    Length: 45 byte(s)
[Original] [Print] [Top]
¶àлÄú£¬limodou´ó¸ç£¬ÎÒ×ÔÔÙÊÔÊÔ£¬¶àл£¬¶àл£¡
[Original] [Print] [Top]
Subject: limodou´ó¸ç£¬³ÌÐòÓеãÎÊÌâ
Author: zhyeye    Posted: 2005-07-26 17:58    Length: 1,110 byte(s)
[Original] [Print] [Top]
ÄãÕâÑùʹÓÃÔª×éÖ®ºó¾Í³öÏÖÁËÒ»¸öÎÊÌ⣬ÔÚscore×Ö¶ÎÖÐ×ÜÊÇ»á³öÏÖÈßÓàµÄÒ»×飬¶øÇÒÿÁ½×éÊý¾Ý¾Í»áÓÐÒ»×éÈßÓ࣬ÎÒ²»Àí½âÕâÊÇΪʲô£¿ÒòΪÄãÒѾ­Ê¹ÓÃÁËfor i in range(len(s)/2)]£¬ÎªÊ²Ã´»¹ÊÇÁ½Á½×ÔÓÉ×éºÏ£¿ÄúÄܰïÎÒÔÙ¿´¿´Âð£¿
Õæ²»ºÃÒâ˼£¬¸øÄúÌíÁËÕâô¶àÂé·³£¬¶àлÁË£¡
ÏÂÃæÊÇÄúÉú³ÉµÄÊý¾Ý×飺
Xiaoyu female 20 [('english', '80'), ('80', 'maths'), ('maths', '88')] [('song',
'44'), ('yang', '45')] Beijing
xiaoming male 20 [('english', '82'), ('82', 'maths')] [('huang', '44'), ('li', '45')]
Beijing
dawei male 21 [('english', '85'), ('85', 'maths'), ('maths', '94')] [('he', '46'),
('zhang', '48')] shanghai
xiaoxiao male 20 [('geography', '87')] [('lv', '44'), ('wang', '45')] Beijing
xiaohong female 21 [('english', '88'), ('88', 'maths'), ('maths', '84'), ('84',
'chinese'), ('chinese', '94')] [('hu', '46'), ('han', '48')] shanghai
feifei male 21 [] [('hu', '46'), ('han', '48')] shanghai
xiaoying female 20 [('english', '80'), ('80', 'maths'), ('maths', '86')] [('li',
'48')] shanghai
[Original] [Print] [Top]
Subject: Re: ÐÁ¿àÄúÁË£¬limodou´ó¸ç£¡
Author: zhyeye    Posted: 2005-07-26 19:32    Length: 387 byte(s)
[Original] [Print] [Top]
ÄǸöÈßÓàÊý¾ÝµÄÎÊÌâ½â¾öÁË£¬
ÎÒ°Ñ
if score:
s = score.split()
score = [(s[i], s[i+1]) for i in range(len(s)/2)]
¸ÄΪ
if score:
s = score.split()
score = [(s[i*2], s[i*2+1]) for i in range(len(s)/2)]
¾Í¿ÉÒÔÁË£¬ÎÒ¹À¼ÆÊÇÄú±ÊÎó£¬ÉÙÇÃÁË*2£¬ÏÖÔÚºÃÁË£¡
¶àлÁË£¬limodou´ó¸ç£¡
[Original] [Print] [Top]
Subject: Re: limodou´ó¸ç£¬³ÌÐòÓеãÎÊÌâ
Author: limodou    Posted: 2005-07-26 22:12    Length: 27 byte(s)
[Original] [Print] [Top]
µÄÈ·ÊÇÈçÄãËù˵Ӧ¸ÃÊÇi*2µÄ¡£
----
[Original] [Print] [Top]
Subject: limodou´ó¸ç£¬ÔÙ´ÎÇëÇó°ïÖú£¡
Author: zhyeye    Posted: 2005-07-27 00:23    Length: 2,562 byte(s)
[Original] [Print] [Top]
limodou´ó¸ç£¬ÎÒ»¹ÊÇŪ²»ºÃ£¬Äã°ïÎÒ¿´¿´£¬ÏÂÃæÊÔÎÒ
ºÏ²¢ºóµÄ³ÌÐò£¬ºÍ³öÏֵĴíÎóÌáʾ£»
³ÌÐò£»

import sys, string
header = """<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?>
<xueshengzhuangtai>"""

body="""<number1>
<name>%(name)s</name>
<gender>%(sex)s</gender>
<age>%(age)s</age>
<result_exam>
<course>%(course)s</course>
<score>%(score)s</score>
</result_exam>
<family>
<parent>%(parent)s</parent>
<age>%(parent_age)s</age>
</family>
<city>%(city)s</city>
</number1>"""

footer="""</xueshengzhuangtai>"""

def deal_body():

for line in file('/yejy/bioinf/d.txt'):
v = [x.strip() for x in line.rstrip().split('*')][:-1]
name, sex, age, result_exam, family, city = tuple(v)
if result_exam:
r = result_exam.split()
result_exam = [(r[i*2], r[i*2+1]) for i in range(len(r)/2)]
course = r[i*2]
score = r[i*2+1]
else:
result_exam = []
if family:
f = family.split()
family = [(f[i*2], f[i*2+1]) for i in range(len(f)/2)]
parent = f[i*2]
parent_age = f[i*2+1]
else:
family = []
print body % locals()

if __name__ == '__main__':
print header
deal_body()
print footer

´íÎóÌáʾ£»
Traceback (most recent call last):
File "C:/Program Files/python/test10.py", line 47, in ?
print body % locals()
KeyError: 'name'
ÎÒÒѾ­ÒýÈëÄ£¿éÁË£¬ËùÒÔÎÒ²»ÖªµÀ¸ÃÊÇʲôÑù×Ó£¿
Âé·³ÄúÁË£¡
[Original] [Print] [Top]
Subject: Re: limodou´ó¸ç£¬ÔÙ´ÎÇëÇó°ïÖú£¡
Author: passworld    Posted: 2005-07-27 01:09    Length: 65 byte(s)
[Original] [Print] [Top]
ÄãµÄ print body ÔÚʲôµØ·½£¿Ó¦¸ÃÔÚ for ÀïÃæ¡£

----
[Original] [Print] [Top]
Subject: Re: limodou´ó¸ç£¬ÔÙ´ÎÇëÇó°ïÖú£¡
Author: limodou    Posted: 2005-07-27 09:01    Length: 40 byte(s)
[Original] [Print] [Top]
ÈçpassworldËù˵Ëõ½ü²»¶Ô£¬Ó¦¸ÃÓëforͬ¼¶¡£
----
[Original] [Print] [Top]
Subject: »ØpassworldºÍlimodou´ó¸ç£¬¶¼ÓеãÎÊÌâ
Author: zhyeye    Posted: 2005-07-27 16:20    Length: 6,815 byte(s)
[Original] [Print] [Top]
ÎҰѶþλµÄ½¨ÒéºÍÊä³öµÄ½á¹ûÌùÉÏÀ´£¬ÄãÃÇÔÙ°ïæ¿´¿´£¬¶àлÁË£¡
Ê×ÏÈÊÇlimodou´ó¸çprint bodyÓë forͬ¼¶µÄ½á¹û£º
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?>
<xueshengzhuangtai>
<number1>
<name>xiaoying</name>
<gender>female</gender>
<age>20</age>
<result_exam>
<course>geography</course>
<score>90</score>
</result_exam>
<family>
<parent>han</parent>
<age>48</age>
</family>
<city>shanghai</city>
</number1>
</xueshengzhuangtai>
½Ó×ÅÊÇpassworldµÄÔÚforÀïÃæ£º
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?>
<xueshengzhuangtai>
<number1>
<name>Xiaoyu</name>
<gender>female</gender>
<age>20</age>
<result_exam>
<course>chinese</course>
<score>92</score>
</result_exam>
<family>
<parent>yang</parent>
<age>45</age>
</family>
<city>Beijing</city>
</number1>
<number1>
<name>xiaoming</name>
<gender>male</gender>
<age>20</age>
<result_exam>
<course>maths</course>
<score>84</score>
</result_exam>
<family>
<parent>li</parent>
<age>45</age>
</family>
<city>Beijing</city>
</number1>
<number1>
<name>dawei</name>
<gender>male</gender>
<age>21</age>
<result_exam>
<course>chinese</course>
<score>89</score>
</result_exam>
<family>
<parent>zhang</parent>
<age>48</age>
</family>
<city>shanghai</city>
</number1>
<number1>
<name>xiaoxiao</name>
<gender>male</gender>
<age>20</age>
<result_exam>
<course>geography</course>
<score>87</score>
</result_exam>
<family>
<parent>wang</parent>
<age>45</age>
</family>
<city>Beijing</city>
</number1>
<number1>
<name>xiaohong</name>
<gender>female</gender>
<age>21</age>
<result_exam>
<course>history</course>
<score>78</score>
</result_exam>
<family>
<parent>han</parent>
<age>48</age>
</family>
<city>shanghai</city>
</number1>
<number1>
<name>feifei</name>
<gender>male</gender>
<age>21</age>
<result_exam>
<course>history</course>
<score>78</score>
</result_exam>
<family>
<parent>han</parent>
<age>48</age>
</family>
<city>shanghai</city>
</number1>
<number1>
<name>xiaoying</name>
<gender>female</gender>
<age>20</age>
<result_exam>
<course>geography</course>
<score>90</score>
</result_exam>
<family>
<parent>han</parent>
<age>48</age>
</family>
<city>shanghai</city>
</number1>
</xueshengzhuangtai>
Óëforͬ¼¶Ê±Ö»Êä³öÁË×îºóÒ»Ìõ¼Ç¼£¬¶øÔÚforÀïÃæÊ±ÔòÊä³öÁËËùÓмǼ£¬ËùÒÔ£¬ÎÒÏëÓ¦¸ÃÊÇÔÚforÀïÃæ£¬µ«ÊÇ£¬»¹ÓÐÒ»¸öÎÊÌ⣬¾ÍÊÇΪʲôËùÓеÄ
<result_exam>
<course>geography</course>
<score>90</score>
</result_exam>
<family>
<parent>han</parent>
<age>48</age>
</family>
¶¼Ö»ÊÇÏÔʾһ×éÊý¾Ý£¬ÊDz»ÊÇÎÒµÄbodyÓÐÎÊÌ⣿
Çë°ïæ¿´¿´£¡¶àл£¬¶àл£¡
[Original] [Print] [Top]
Subject: Re: »ØpassworldºÍlimodou´ó¸ç£¬¶¼ÓеãÎÊÌâ
Author: passworld    Posted: 2005-07-27 16:51    Length: 430 byte(s)
[Original] [Print] [Top]
ÄãÕâҲ̫ËÀ¶ÁÊéÁ˰ɣ¿ÄãÐèÒªÒ»¸öÑ­»·»òÕߺ¯ÊýרÃÅ´¦Àí·ÖÊý£º


def deal_result(result):
ret = []
for i in result:
ret.append('''
<course>%s</course>
<score>%s</score>''' % (i[0], i[1]))
return ' '.join(ret)



----
[Original] [Print] [Top]
Subject: Re: »ØpassworldºÍlimodou´ó¸ç£¬¶¼ÓеãÎÊÌâ
Author: zhyeye    Posted: 2005-07-27 17:55    Length: 4,251 byte(s)
[Original] [Print] [Top]
ÈõÈõµÄÎÊ£ºÎÒÓ¦¸Ã°ÑËü·ÅÔÚʲôλÖÃÄØ£¿ÎÒ³¢ÊÔÁË·ÅÔÚprint body֮ǰºÍÖ®ºó£¬»¹ÓзÅÔÚelseÖ®ºó¶¼²»Æð×÷Ó㬳öÏÖ´íÎó£¬ÄúÄܸæËßÎÒÕâÊÇÔõô»ØÊÂÂð£¿
¶àлÁË£¡
ÏÂÃæÊÔÎÒ¼ÓÁËÖ®ºóµÄ³ÌÐò(#ºóÊǵÚÒ»´Î·ÅµÄλÖÃ)£»
import sys, string
header = """<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?>
<xueshengzhuangtai>"""

body="""<number1>
<name>%(name)s</name>
<gender>%(sex)s</gender>
<age>%(age)s</age>
<result_exam>
<course>%(course)s</course>
<score>%(score)s</score>
</result_exam>
<family>
<parent>%(parent)s</parent>
<age>%(parent_age)s</age>
</family>
<city>%(city)s</city>
</number1>"""

footer="""</xueshengzhuangtai>"""

def deal_body():

for line in file('/yejy/bioinf/d.txt'):
v = [x.strip() for x in line.rstrip().split('*')][:-1]
name, sex, age, result_exam, family, city = tuple(v)
if result_exam:
r = result_exam.split()
result_exam = [(r[i*2], r[i*2+1]) for i in range(len(r)/2)]
course = r[i*2]
score = r[i*2+1]
else:
result_exam = []
course = ''
score = ''

if family:
f = family.split()
family = [(f[i*2], f[i*2+1]) for i in range(len(f)/2)]
parent = f[i*2]
parent_age = f[i*2+1]
else:
family = []
parent = ''
parent_age = ''

def deal_result_exam(result_exam):
result_exam = []
for i in result_exam:
result_exam.append('''
<course>%s</course>
<score>%s</score>''' % (i[0], i[1]))
return ' '.join(result_exam)
def deal_family(family):
family= []
for i in family:
family.append('''
<parent>%s</parent>
<age>%s</age>''' % (i[0], i[1]))
return ' '.join(family)

print body % locals()
#def deal_result_exam(result_exam):
#result_exam = []
#for i in result_exam:
#result_exam.append('''
#<course>%s</course>
#<score>%s</score>''' % (i[0], i[1]))
#return ' '.join(result_exam)
#def deal_family(family):
#family = []
#for i in family:
#family.append('''
#<parent>%s</parent>
#<age>%s</age>''' % (i[0], i[1]))
# return ' '.join(family)

if __name__ == '__main__':
print header
deal_result_exam(result_exam)
deal_family(family)
deal_body()
print footer
´íÎóÏÔʾ£º
Traceback (most recent call last):
File "C:Program Filespython est10.py", line 82, in ?
deal_result_exam(result_exam)
NameError: name 'result_exam' is not defined
[Original] [Print] [Top]
Subject: Re: »ØpassworldºÍlimodou´ó¸ç£¬¶¼ÓеãÎÊÌâ
Author: passworld    Posted: 2005-07-27 19:47    Length: 3,317 byte(s)
[Original] [Print] [Top]
¿´À´ÄãÕæÊÇÒ»µãpython¶¼²»»á£¬×öÍêÕâ¸öÏîÄ¿ºóºÃºÃѧѧpython°É£¬ºÜÓÐÓõÄÒ»¸öÓïÑÔ£º


#!/usr/bin/python
import sys, string
header = """&lt;?xml version = "1.0"?&gt;
&lt;?xml:stylesheet type = "text/xsl"
href = "xszt.xsl"?&gt;
&lt;xueshengzhuangtai&gt;"""

body="""&lt;number1&gt;
&lt;name&gt;%(name)s&lt;/name&gt;
&lt;gender&gt;%(sex)s&lt;/gender&gt;
&lt;age&gt;%(age)s&lt;/age&gt;
&lt;result_exam&gt;
%(results)s
&lt;/result_exam&gt;
&lt;family&gt;
%(parents)s
&lt;/family&gt;
&lt;city&gt;%(city)s&lt;/city&gt;
&lt;/number1&gt;"""

footer="""&lt;/xueshengzhuangtai&gt;"""
FNAME='/yejy/bioinf/d.txt'
def deal_body(fname):

try:
for line in file(fname):
v = [x.strip() for x in line.rstrip().split('*')][:-1]
name, sex, age, result_exam, family, city = tuple(v)
if result_exam:
r = result_exam.split()
result_exam = [(r[i*2], r[i*2+1]) for i in range(len(r)/2)]
else:
result_exam = []
if family:
f = family.split()
family = [(f[i*2], f[i*2+1]) for i in range(len(f)/2)]
else:
family = []

results = deal_result_exam(result_exam)
parents = deal_family(family)

print body % locals()
except:
print 'Error:', line
print sys.exc_info()[1]
sys.exit()

def deal_result_exam(results):
result_exam = []
for i in results:
result_exam.append('''
&lt;course&gt;%s&lt;/course&gt;
&lt;score&gt;%s&lt;/score&gt;''' % (i[0], i[1]))
return ' '.join(result_exam)

def deal_family(family):
result_family= []
for i in family:
result_family.append('''
&lt;parent&gt;%s&lt;/parent&gt;
&lt;age&gt;%s&lt;/age&gt;''' % (i[0], i[1]))
return ' '.join(result_family)

if __name__ == '__main__':
print header
if len(sys.argv) == 2:
fname = sys.argv[1]
else:
fname = FNAME
deal_body(fname)
print footer



----
[Original] [Print] [Top]
Subject: Re: »ØpassworldºÍlimodou´ó¸ç£¬¶¼ÓеãÎÊÌâ
Author: passworld    Posted: 2005-07-27 19:48    Length: 8 byte(s)
[Original] [Print] [Top]
¸½¼þÔ´Âë
----
--
Attached file: 569988-doexam.txt
[Original] [Print] [Top]
Subject: Ì«¸ÐлÄúÁË£¡
Author: zhyeye    Posted: 2005-07-27 21:04    Length: 181 byte(s)
[Original] [Print] [Top]
ÎÒµÄȷʲô¶¼²»¶®£¬²Å¸Õ¸Õ¿ªÊ¼Ñ§£¬µ«ÊÇÀϰå·ÇÒªÈÃÎÒÍê³ÉÕâ¸öÈÎÎñ£¬ËùÒԲŸøÄúºÍlimodou´ó¸çÌíÁËÕâô¶àÂé·³£¬ÕæµÄÌ«¸ÐлÄãÃÇÁË¡£pythonÕæµÄºÜÓÐÒâ˼£¬ÎÒÒ»¶¨»áºÃºÃѧϰµÄ¡£
¶àлÄãÃÇÁË£¡
[Original] [Print] [Top]
Subject: Re: Ì«¸ÐлÄúÁË£¡
Author: limodou    Posted: 2005-07-27 23:09    Length: 266 byte(s)
[Original] [Print] [Top]
ŬÁ¦Ñ§°É¡£ÆäʵÄãµÄÎÊÌâÈç¹ûת»»³ÉÄ£°å¹¤×÷»á·Ç³£¼òµ¥£¬µ±È»Ë¼Â·¾Í¸´ÔÓһЩ£¬¶øÇÒ»¹ÒªÓõ½ÆäËüµÄ°ü¡£µ«ÊÇʹÓÃÄ£°åµÄºÃ´¦ÊÇ£ºÄ£°åÓÃÓÚ´¦ÀíÏÔʾ£¬ÄãµÄÈÎÎñ¾ÍÊÇ·ÖÎö³ö¸½ºÏÄ£°åµÄÊý¾Ý½á¹¹£¬È»ºóÓëÄ£°åÏà½áºÏ¡£ÕâÑùÄãµÄÖ÷Òª¹Ø×¢µã¾ÍÊÇÈçºÎ½âÎöԭʼÊý¾Ý£¬È»ºóÈçºÎÉú³É¸½ºÏÄ£°åÒªÇóµÄÊý¾Ý½á¹¹¡£
----
[Original] [Print] [Top]
Subject: ¹ØÓÚÄ£°å
Author: alula    Posted: 2005-07-28 12:00    Length: 7,245 byte(s)
[Original] [Print] [Top]
target="_blank">http://www.donews.net/limodou/archive/2004/11/10/166282.aspx
ÔÚÉÏÃæµÄÁ´½Ó£¬ÎÒ¿´ÁËÄú½éÉÜMeteorµÄÎÄÕ¡£Ä¿Ç°Ä£°å±È½ÏÓÐÐËȤ¡£
Èç¹ûÓÃMeteor¸ÉÕâ¸ö»î¶ù£¬Ð´³öÀ´µÄ´úÂë´ó¸ÅÊÇʲô¸öÄ£ÑùÄØ£¿

ÎÒÐÞ¸ÄÁË×Ô¼ºµÄÄ£°å·ÖÎö³ÌÐò£¬¸ÉÕâ¸ö»î¶ù£¬´úÂëÊÇÕâ¸öÑù×ӵģº


#! /usr/bin/python

import re, string, sys
from StringIO import StringIO

keywords = 'end', 'template', 'if', 'elif', 'else', 'apply-template', 'include'

_varname = re.compile('${(w+)}')
_meta = re.compile('^s*<!--s*#s*(%s)(.*)-->s*$' % '|'.join(keywords))

def _G_repl(txt, dct):
return re.sub(_varname, lambda g: str(dct.get(g.group(1))), txt)

def _G_meta(line):
if line.lstrip()[:4] == '<!--':
res = re.search(_meta, line)
if res:
return res.group(1, 2)
return 0, 0

class THtml:
def __init__(self, src):
self._src = src
def begin(self, dct):
line = self._src.readline()
while line:
if self.recv(line, dct) == 0:
break
line = self._src.readline()
def recv(self, line, dct):
key, exp = _G_meta(line)
if key:
return self.statement(key, exp, dct)
return self.normal(line, dct)
def statement(self, key, exp, dct):
assert key in ('if', 'template', 'apply-template', 'include')
if key == 'if':
exp = eval(_G_repl(exp[:exp.rindex(':')], dct))
If(self._src, exp).begin(dct)
elif key == 'template':
exp = eval(_G_repl(exp[:exp.rindex(':')], dct))
txt = []
Extract(self._src, txt).begin(dct)
txt.pop()
_templates[exp] = StringIO(''.join(txt))
elif key == 'include':
exp = eval(_G_repl(exp, dct))
xf = file(exp)
THtml(xf).begin(dct)
elif key == 'apply-template':
exp = eval(_G_repl(exp, dct))
vf = _templates[exp]
for d in dct[exp]:
THtml(vf).begin(d)
vf.seek(0)
return 1
def normal(self, line, dct):
sys.stdout.write(_G_repl(line, dct))
return 1

class Skip(THtml):
def __init__(self, src):
THtml.__init__(self, src)
def statement(self, key, exp, dct):
if key == 'end':
return 0
elif key in ('if', 'template'):
Skip(self._src).begin(dct)
return 1
def normal(self, line, dct):
return 1

class Extract(THtml):
def __init__(self, src, lines):
THtml.__init__(self, src)
self._lines = lines
def recv(self, line, dct):
self._lines.append(line)
return THtml.recv(self, line, dct)
def statement(self, key, exp, dct):
if key == 'end':
return 0
elif key in ('if', 'template'):
Extract(self._src, self._lines).begin(dct)
return 1
def normal(self, line, dct):
return 1

class If(THtml):
def __init__(self, src, bool):
THtml.__init__(self, src)
self._true = bool
def statement(self, key, exp, dct):
if key == 'end':
return 0
elif key in ('elif', 'else'):
if self._true:
Skip(self._src).begin(dct)
else:
if key == 'elif':
self._true = eval(_G_repl((exp[: exp.rindex(':')]), dct))
else:
self._true = 1
else:
if self._true:
return THtml.statement(self, key, exp, dct)
elif key in ('if', 'template'):
Skip(self._src).begin(dct)
return 1
def normal(self, line, dct):
if self._true:
THtml.normal(self, line, dct)
return 1

_dicts = {}
_templates = {}

from StringIO import StringIO

def printf(vf, glob):
if type(vf) == str:
THtml(file(vf)).begin(glob)
else:
THtml(vf).begin(glob)

def test():
content = """<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "xszt.xsl"?>

<!-- #template 'glob': -->
<number1>
<name>${name}</name>
<gender>${sex}</gender>
<age>${age}</age>
<result_exam>
<!-- #template 'result_exam': -->
<subject>${subject}</subject>
<score>${score}</score>
<!-- #end -->
<!-- #apply-template 'result_exam' -->
</result_exam>
<family>
<!-- #template 'family': -->
<name>${name}</name>
<age>${age}</age>
<!-- #end -->
<!-- #apply-template 'family' -->
</family>
<city>${city}</city>
</number1>
<!-- #end -->

<xueshengzhuangtai>
<!-- #apply-template 'glob' -->
</xueshengzhuangtai>
"""