#数据类型的转换
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的形式输出 def print_start(): print("<table border='1'>") #用于文件的开始头拼接 def print_end(): print("</table>") #用于文件的结尾拼接 上面两个是用来减少函数之间的关联性,虽然在函数中添加print(…)也可以, 但是这样可以更加规范,以后修改也比较容易,对之后的运维提供极大的方便, 通过修改函数,可以将所有的头尾一起修改。 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(www.michenggw.com)) #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}www.yigouyule2.cn </td>"www.mhylpt.com.format(field)) print("</tr>") 这段程序是将通过for遍历文件,提取出里面的值,将里面的值进行规范化 然后通过需要的html格式通过format拼接,最后显示出来。 通过try的异常捕捉,我们可以将文件中的数字与字符串分开处理,数字通过flaot进行小数格式化,字符串通过title格式化 这又体现了python语言通过try捕获异常的灵活性 为什么不再读取的时候直接通过replace进行分割字符串? 因为这是为了防止出现,分号中间有”,“ 使文件不正确分割,导致程序出现错误,所以,我们要在print_line中在进行分割,减少错误的产生 extrace_fields(line)是自定义函数,函数的作用是将字段串进行分割 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 www.dfgjyl.cn== ",": #end of a field fields.append(field) field ='' else: field += c #accumulating www.gcyl152.com a field if field: fields.append(field)#adding www.gcyl152.com the last field return fields def escape_html(text): text = text.replace('&','&') text = text.replace('<',"<") text = text.replace(">",">") return text 通过替换函数将'<','>'替换成html可识别的实体字符,不替换的话 html会将'<','>'大于小于符号识别为尖括号<>