简体   繁体   English

Python,用于从两个指定字符串之间的文件中读取信息(当这些字符串可以出现在其他位置时)

[英]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. 在这种情况下,我有一个包含[DeviceA,DeviceA2,DeviceB]的列表,并且我想以粗体显示输出(因此,当显示设备名称时,然后显示“ 其他信息” 。设备名称稍后可能会再次出现在文本文件。

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时:输出应为(设备名称可以忽略,只需要设备名称和其他信息之间的信息

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

when DeviceB: 当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. 使用index查找所需设备的索引,然后再次使用index(将所需设备的索引作为第二个参数)查找以下“参数值:”,然后再次查找“其他信息”,然后取子字符串。

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. 当然,您需要将此功能附加到每个设备的函数中,并且还需要通过文件开头声明的初始Device,但是如果您了解上述内容的工作原理,这应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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