⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pyxml.py

📁 python改写的<<C语言百例>>! 很经典的程序开发例子!
💻 PY
字号:
#making up a text file's data as XML

import sys

print "Content-type:text/xml\n"

#write XML declaration and processing instruction
print """<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl"
href = "name.xsl"?>"""

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

print "<contacts>"  #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 lastname and firstname
    last, first = currentLine.split( "," )
    first = first.strip()   #remove carriage return

    #write contact element
    print """ <contact>
    <LastName>%s</LastName>
    <FirstName>%s</FirstName>
    </contact>""" % ( last, first )

file.close()

print "</contacts>"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -