免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3563 | 回复: 10
打印 上一主题 下一主题

python 如何利用一个词查找相关内容 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-05-18 15:48 |只看该作者 |倒序浏览
        <Analog NoInSource="224" maxValue="300.0000" minValue="0.0000" name="RBC-VIA接口柜1电压" show_name="RBC-VIA接口柜1电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
        <Analog NoInSource="225" maxValue="20.0000" minValue="0.0000" name="RBC-VIA接口柜1电流" show_name="RBC-VIA接口柜1电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
        <Analog NoInSource="226" maxValue="300.0000" minValue="0.0000" name="RBC-VIA接口柜2电压" show_name="RBC-VIA接口柜2电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
        <Analog NoInSource="227" maxValue="20.0000" minValue="0.0000" name="RBC-VIA接口柜2电流" show_name="RBC-VIA接口柜2电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
        <Analog NoInSource="228" maxValue="300.0000" minValue="0.0000" name="RBC-临时限速1电压" show_name="RBC-临时限速1电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
        <Analog NoInSource="229" maxValue="20.0000" minValue="0.0000" name="RBC-临时限速1电流" show_name="RBC-临时限速1电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
        <Analog NoInSource="230" maxValue="300.0000" minValue="0.0000" name="RBC-临时限速2电压" show_name="RBC-临时限速2电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
        <Analog NoInSource="231" maxValue="20.0000" minValue="0.0000" name="RBC-临时限速2电流" show_name="RBC-临时限速2电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
        <Analog NoInSource="232" maxValue="300.0000" minValue="0.0000" name="RBC-临时限速3电压" show_name="RBC-临时限速3电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
        <Analog NoInSource="233" maxValue="20.0000" minValue="0.0000" name="RBC-临时限速3电流" show_name="RBC-临时限速3电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
         我只想打印关于电压的字符串

论坛徽章:
0
2 [报告]
发表于 2016-05-18 15:49 |只看该作者
新手小白求告知,谢谢

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:55:28IT运维版块每日发帖之星
日期:2016-07-29 06:20:00
3 [报告]
发表于 2016-05-18 17:25 |只看该作者
试试 re模块中的 search 方法

论坛徽章:
0
4 [报告]
发表于 2016-05-18 17:38 |只看该作者
应该可以只是我是小白,打印了别的东西 还求具体一些

论坛徽章:
4
程序设计版块每日发帖之星
日期:2015-10-14 06:20:00每日论坛发贴之星
日期:2015-10-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-02 06:20:00程序设计版块每日发帖之星
日期:2016-05-08 06:20:00
5 [报告]
发表于 2016-05-19 11:53 |只看该作者
给个你想要的结果看看

论坛徽章:
0
6 [报告]
发表于 2016-05-20 16:05 |只看该作者
回复 5# mswsg


RBC-VIA接口柜1电压
RBC-VIA接口柜2电压
RBC-临时限速1电压
RBC-临时限速2电压
RBC-临时限速3电压

论坛徽章:
0
7 [报告]
发表于 2016-05-20 21:14 |只看该作者
回复 1# 1747768292
  1. # encoding: utf-8
  2. data = """       <Analog NoInSource="224" maxValue="300.0000" minValue="0.0000" name="RBC-VIA接口柜1电压" show_name="RBC-VIA接口柜1电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
  3.         <Analog NoInSource="225" maxValue="20.0000" minValue="0.0000" name="RBC-VIA接口柜1电流" show_name="RBC-VIA接口柜1电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
  4.         <Analog NoInSource="226" maxValue="300.0000" minValue="0.0000" name="RBC-VIA接口柜2电压" show_name="RBC-VIA接口柜2电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
  5.         <Analog NoInSource="227" maxValue="20.0000" minValue="0.0000" name="RBC-VIA接口柜2电流" show_name="RBC-VIA接口柜2电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
  6.         <Analog NoInSource="228" maxValue="300.0000" minValue="0.0000" name="RBC-临时限速1电压" show_name="RBC-临时限速1电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
  7.         <Analog NoInSource="229" maxValue="20.0000" minValue="0.0000" name="RBC-临时限速1电流" show_name="RBC-临时限速1电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
  8.         <Analog NoInSource="230" maxValue="300.0000" minValue="0.0000" name="RBC-临时限速2电压" show_name="RBC-临时限速2电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
  9.         <Analog NoInSource="231" maxValue="20.0000" minValue="0.0000" name="RBC-临时限速2电流" show_name="RBC-临时限速2电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>
  10.         <Analog NoInSource="232" maxValue="300.0000" minValue="0.0000" name="RBC-临时限速3电压" show_name="RBC-临时限速3电压" thresholdDown="176.0000" thresholdUpper="253.0000" unit="V"/>
  11.         <Analog NoInSource="233" maxValue="20.0000" minValue="0.0000" name="RBC-临时限速3电流" show_name="RBC-临时限速3电流" thresholdDown="0.0000" thresholdUpper="10.0000" unit="A"/>"""

  12. import re

  13. line = re.compile("<Analog (.*)/>")
  14. kv = re.compile('(\w+)="(\S+)"')

  15. for l in line.findall(data):
  16.     this = {}
  17.     for k,v in kv.findall(l):
  18.         this[k] = v
  19.     if this["unit"] == "V":
  20.         output = [ "=".join([k,v]) for k,v in this.iteritems()]
  21.         print ",".join(output)
复制代码
  1. name=RBC-VIA接口柜1电压,thresholdUpper=253.0000,show_name=RBC-VIA接口柜1电压,maxValue=300.0000,minValue=0.0000,NoInSource=224,thresholdDown=176.0000,unit=V
  2. name=RBC-VIA接口柜2电压,thresholdUpper=253.0000,show_name=RBC-VIA接口柜2电压,maxValue=300.0000,minValue=0.0000,NoInSource=226,thresholdDown=176.0000,unit=V
  3. name=RBC-临时限速1电压,thresholdUpper=253.0000,show_name=RBC-临时限速1电压,maxValue=300.0000,minValue=0.0000,NoInSource=228,thresholdDown=176.0000,unit=V
  4. name=RBC-临时限速2电压,thresholdUpper=253.0000,show_name=RBC-临时限速2电压,maxValue=300.0000,minValue=0.0000,NoInSource=230,thresholdDown=176.0000,unit=V
  5. name=RBC-临时限速3电压,thresholdUpper=253.0000,show_name=RBC-临时限速3电压,maxValue=300.0000,minValue=0.0000,NoInSource=232,thresholdDown=176.0000,unit=V
复制代码

论坛徽章:
4
程序设计版块每日发帖之星
日期:2015-10-14 06:20:00每日论坛发贴之星
日期:2015-10-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-02 06:20:00程序设计版块每日发帖之星
日期:2016-05-08 06:20:00
8 [报告]
发表于 2016-05-21 10:55 |只看该作者
如果我没理解错:找到含有“电压”的行,然后打印name字段
  1. # encoding: utf-8
  2. with open('1.txt', 'r') as f:
  3.     for line in f:
  4.         if '电压' in line:
  5.             line = line.strip().split()
  6.             print line[4][6:-1]
复制代码
结果
  1. RBC-VIA接口柜1电压
  2. RBC-VIA接口柜2电压
  3. RBC-临时限速1电压
  4. RBC-临时限速2电压
  5. RBC-临时限速3电压
复制代码

论坛徽章:
0
9 [报告]
发表于 2016-05-23 09:46 |只看该作者
回复 8# mswsg


    您这是分片方法吧,谢谢啦

论坛徽章:
0
10 [报告]
发表于 2016-05-23 09:47 |只看该作者
回复 7# Hadron74


    谢谢,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP