![](/img/trans.png)
[英]Extract text present in between two strings in a text file using Python
[英]Python for reading information from a file between two specified strings when these strings can be present elsewhere
我有一个文本文件,其中包含某些设备的信息。 我想提取某些特定设备的信息。 我想知道要提取哪个信息的方法是找到特定字符串时。
输入文字档:
Table of Contents:
DeviceA ...... Page..
DeviceA2 ..... Page..
DeviceB ...... page..
DeviceB2...... Page..
Device1 ...... Page..
Device2 ...... Page..
DeviceC ...... Page..
Blah
Blah
[DeviceA, DeviceA2] Parameter Values:
Width = 1u
length = 2u etc...
Other Information
blah
[Device1] Parameter Values:
Width = 5u
length = 5u etc..
Other Information
blah
[DeviceB, DeviceB2] Parameter Values:
Width = 11u
length = 22u etc..
Other Information
blah
[Device2] Parameter Values:
Width = 5u
length = 5u etc..
Other Information
DeviceA, A2 description
Device1,2 description etc
我有要提取其信息的设备名称列表。 在这种情况下,我有一个包含[DeviceA,DeviceA2,DeviceB]的列表,并且我想以粗体显示输出(因此,当显示设备名称时,然后显示“ 其他信息” 。设备名称稍后可能会再次出现在文本文件。
因此,当选择设备名称DeviceA或DeviceA2时:输出应为(设备名称可以忽略,只需要设备名称和其他信息之间的信息 :
[DeviceA, DeviceA2] Parameter Values:
Width = 1u
length = 2u etc...
Other Information
当DeviceB:
[DeviceB, DeviceB2] Parameter Values:
Width = 1u
length = 2u etc..
Other Information
我尝试在出现“ 其他信息 ”时拆分整个文件,然后通过解析设备名称按设备名称拆分。 但这总是会引起一些问题。 有什么建议么?
谢谢 :)
这是非常令人困惑的英语,但是如果您只想在某些设备的“参数值:”之后和“其他信息”之前输入文本,则需要字符串具有子字符串和索引的方法。
使用index查找所需设备的索引,然后再次使用index(将所需设备的索引作为第二个参数)查找以下“参数值:”,然后再次查找“其他信息”,然后取子字符串。
类似于以下内容:
marker0 = myfile.index("DeviceA")
marker1 = myfile.index("Parameter Values:",marker0)
startpoint = marker1+len("Parameter Values:")
endpoint = myfile.index("Other Information", startpoint)
print("Relevant Information is: "+ myfile.substring(startpoint,endpoint)
当然,您需要将此功能附加到每个设备的函数中,并且还需要通过文件开头声明的初始Device,但是如果您了解上述内容的工作原理,这应该很容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.