经过python将xml文件转换成html文件

#数据类型的转换
def main():
    maxwidth = 100  #用于规范字段的长度
    print_start()
    count=0
    while True:
        try:
            line =input()
            if count == 0:
                color = 'lightgreen'
            elif count % 2#取余
                color = 'white'
            else:
                color = 'lightyellow'
            print_line(line,color,maxwidth)
            count += 1
        except EOFError:
            break
    print_end()

maxwidth 用于规范字段的长度,一旦比这个长度长的字段,咱们能够经过用省略号来替代后面的内容
count 用于对文件中的颜色的改变,斑马纹的实现。
上面的代码中的print_start(),print_line(),print_end()三个函数是咱们本身的设定的函数,代码在后面
print_start() 函数用于输入开头
print_end() 函数用于输入结尾
print_line() 将该行以html的形式输出html

def print_start():
    print("<table border='1'>")
#用于文件的开始头拼接
def print_end():
    print("</table>")
#用于文件的结尾拼接

上面两个是用来减小函数之间的关联性,虽然在函数中添加print(…)也能够,
可是这样能够更加规范,之后修改也比较容易,对以后的运维提供极大的方便,
经过修改函数,能够将全部的头尾一块儿修改。python

def print_line(line,color,maxwidth):
    print("<tr bgcolor='{0}'>".format(color))
    fields = extrace_fields(line)
    for field in fields:
        if not field:
            print("<td></td>")
        else:
            number = field.replace(",","")
            #这里把”,“改为”“的意义是为了将数值1,000转换为1000的格式
        try:
            x = float(number)
            print("<td align='right'>{0}</td>33".format(round(x)))
        except ValueError:
            field =field.title() 
            '''
            用于注释
            title函数是用来将字符串的首字母大写处理            
            str = "this is string example from runoob....wow!!!"
            请注意,非字母后的第一个字母将转换为大写字母:
            txt = "hello b2b2b2 and 3g3g3g"
            print(txt.title())    #Hello B2B2B2 And 3G3G3G
            '''

            field = field.replace('And','and')
            if len(field) <= maxwidth:
                field = escape_html(field)
            else:
                field = "{0}...".format(
                escape_html(field[:maxwidth]))
            print("<td>{0}</td>".format(field))
    print("</tr>")

这段程序是将经过for遍历文件,提取出里面的值,将里面的值进行规范化 而后经过须要的html格式经过format拼接,最后显示出来。
经过try的异常捕捉,咱们能够将文件中的数字与字符串分开处理,数字经过flaot进行小数格式化,字符串经过title格式化
这又体现了python语言经过try捕获异常的灵活性
为何再也不读取的时候直接经过replace进行分割字符串?
由于这是为了防止出现,分号中间有”,“ 使文件不正确分割,致使程序出现错误,因此,咱们要在print_line中在进行分割,减小错误的产生
extrace_fields(line)是自定义函数,函数的做用是将字段串进行分割apache

def extrace_fields(line):
    fields =[]
    field = ''
    quote = None
    for c in line:
        if c in "\"":    
            if quote is None:  #start of quoted string
                quote = c
            elif quote == c: #end of quoted string
                quote = None
            else:
                field += c #other quote inside quoted string
            continue 
        if quote is None and c == ","#end of a field
            fields.append(field)
            field =''
        else:
            field += c  #accumulating a field
    if field:
        fields.append(field)#adding the last field
    return fields
def escape_html(text):
    text = text.replace('&','&amp;')
    text = text.replace('<',"&lt;")
    text = text.replace(">","&gt;")
    return text

经过替换函数将'<','>'替换成html可识别的实体字符,不替换的话 html会将'<','>'大于小于符号识别为尖括号<>app

相关文章
相关标签/搜索