【发布时间】:2018-08-02 01:41:06
【问题描述】:
我想阅读文本文件并在下面的文本中找到以 5 开头的单词。
“状态”:“OK”}],“PhysicalLocationString”:“rack1/chassis_u1/cpu0/pcie_slot3”,“Physloc”:“0003000101010005”,“PlaceholderNetworkDeviceFunctions”:[{“FC HBA 1”:“51: 40:2E:C0:01:C9:53:E8"}, {"FC HBA 2": "51:40:2E:C0:01:C9:53:EA"}]}}, "零件编号": " P9D94A”、“SerialNumber”:“MY57470DS8”、“状态”:{“Health”:“OK”、“HealthRollup”:“OK”、“State”:“Enabled”}}
我在另一个文件中的输出应该是
51:40:2E:C0:01:C9:53:E8 51:40:2E:C0:01:C9:53:EA
这是我的代码:
import re
import sys
import os
#get line
with open('/root/SDFlex/work/cookbooks/ilorest/files/OP.txt', 'r') as file:
for line in file:
re.findall(r'\b[s]:\w+', line)
matchedLine = line
break
#and write it to the file
with open('/root/SDFlex/work/cookbooks/ilorest/files/file.txt', 'w') as
file:
file.write(matchedLine)
请帮忙
【问题讨论】:
-
这看起来像 JSON 内容,因此您应该在这里使用 JSON 解析器,而不是纯正则表达式解决方案。