經過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

相關文章
相關標籤/搜索