简体   繁体   中英

Python for reading information from a file between two specified strings when these strings can be present elsewhere

I have a text file from which contains information for some devices. I would like to extract information for some of the specific devices. The way I want to know which info to extract is when a specific string is found.

Input text file:

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

I have a list of device names for which I want to extract the info. in this case I have a list that contains [DeviceA, DeviceA2, DeviceB] and I want to get the output in bold (so when the name of the device appear and then Other Information . The name of the device may appear again later on in the text file.

So when device name DeviceA or DeviceA2 is chosen: the output should be (device name can be ignored, only the information between device name and Other Information is desired:

[DeviceA, DeviceA2] Parameter Values:
Width = 1u
length = 2u etc...
Other Information

when DeviceB:

[DeviceB, DeviceB2] Parameter Values:
Width = 1u
length = 2u etc..
Other Information

I tried splitting the entire file when Other Information appears then splitting by the device names by parsing the device names. But this always causes some issue or another. Any suggestions?

thank you :)

This is very confusing English but if you just want the text for after "Parameter Values:" and before "Other Information" for certain Devices, then the methods string has of substring and index are what you need.

Use index to find the index of the Device you want, then use index again (with the index of the device you want as 2nd argument) to find the following "Parameter values:", then again to find "other information" then just take the substrings.

Something along the lines of:

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)

Of course you need to stick this into a function to do it for each device, and also you need to get past the initial Device declared at the start of the file but that should be easy if you understand how the above works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM