博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过python将xml文件转换成html文件
阅读量:7014 次
发布时间:2019-06-28

本文共 2996 字,大约阅读时间需要 9 分钟。

hot3.png

  #数据类型的转换

  
  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('&','&amp;')
  
  text = text.replace('<',"&lt;")
  
  text = text.replace(">","&gt;")
  
  return text
  
  通过替换函数将'<','>'替换成html可识别的实体字符,不替换的话 html会将'<','>'大于小于符号识别为尖括号<>

转载于:https://my.oschina.net/u/3386278/blog/2937482

你可能感兴趣的文章
线程使用三——future和callable详解
查看>>
Logstash + DataHub + MaxCompute/StreamCompute 进行实时数据分析
查看>>
阿里云数加大数据计算服务MaxCompute学习路线图 (持续更新中)
查看>>
聊聊springcloud的GatewayControllerEndpoint
查看>>
聊聊sentinel的SentinelResourceAspect
查看>>
聊聊flink的SpoutWrapper
查看>>
聊聊flink的StateDescriptor
查看>>
git 使用教程,常用命令
查看>>
Android view事件分发初步
查看>>
使用SVI实现Vlan间路由
查看>>
Linux学习笔记5月28日任务
查看>>
解决Td内容为空时不显示边框的问题-兼容IE、firefox、chrome
查看>>
SylixOS x86中断探测(二)
查看>>
HDFS总结
查看>>
scala 中导出excel
查看>>
悬浮框保证在中间js
查看>>
Nginx负载均衡算法
查看>>
Linux的目录结构
查看>>
linux基础命令
查看>>
Nginx服务器
查看>>