我有一个文本文件,其中包含一些设备的信息。我想提取一些特定设备的信息。我想知道提取哪种信息的方式是在找到特定的字符串时。
输入文本文件:
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和我想要以粗体显示输出的列表(所以当设备的名称出现时,然后是Other Information )。设备的名称可能会在稍后的文本文件中再次出现。
因此,当选择设备名称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当其他信息出现时,我尝试拆分整个文件,然后通过解析设备名来拆分设备名。但这总是会引起一些问题。有什么建议吗?
(谢谢:)
发布于 2013-02-05 14:36:27
这是非常令人困惑的英语,但如果您只想要后面的文本“参数值:”和“其他信息”之前的某些设备,那么方法字符串的子字符串和索引是您所需要的。
使用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)当然,您需要将它添加到一个函数中,以便对每个设备执行此操作,并且您还需要通过在文件开始时声明的初始设备,但是如果您了解上面的工作原理,那么这应该很容易。
https://stackoverflow.com/questions/14709534
复制相似问题