简体   繁体   English

如何从文本文件中提取特定行

[英]How to extract a specific line out of a text file

I have the code from the attached picture in a.can-file, which is in this case a text file.我在 a.can 文件中有来自附件图片的代码,在本例中是一个文本文件。 The task is to open the file and extract the content of the void function.任务是打开文件并提取 void function 的内容。 In this case it would be "$LIN::Kl_15 = 1;"在这种情况下,它将是“$LIN::Kl_15 = 1;” 在此处输入图像描述

This is what I already got:这是我已经得到的:

Masterfunktionsliste = open("C:/.../Masterfunktionsliste_Beispiel.can", "r")
Funktionen = []
Funktionen = Masterfunktionsliste.read() 
Funktionen = Funktionen.split('\n')
print(Funktionen)

I receive the following list:我收到以下列表:

['', '', 'void KL15 ein', '{', '\t$LIN::Kl_15 = 1;', '}', '', 'void Motor ein', '{', '\t$LIN::Motor = 1;', '}', '', '']

And now i want to extract the $LIN::Kl_15 = 1;现在我想提取$LIN::Kl_15 = 1; and the $LIN::Motor = 1;$LIN::Motor = 1; line into variables.行成变量。

Use the { and } lines to decide what lines to extract:使用{}行来决定要提取哪些行:

scope_depth = 0
line_stack = list(reversed(Funktionen))
body_lines = []

while len(line_stack) > 0:
    next = line_stack.pop()
    if next == '{':
        scope_depth = scope_depth + 1
    elif next == '}':
        scope_depth = scope_depth - 1
    else:
        # test that we're inside at lest one level of {...} nesting
        if scope_depth > 0:
            body_lines.append(next)

body_lines should now have values ['$LIN::Kl_15 = 1;', '$LIN::Motor = 1;'] body_lines现在应该有值['$LIN::Kl_15 = 1;', '$LIN::Motor = 1;']

You can loop through the list, search for your variables and save it as dict:您可以遍历列表,搜索变量并将其保存为 dict:

can_file_content = ['', '', 'void KL15 ein', '{', '\t$LIN::Kl_15 = 1;', '}', '', 'void Motor ein', '{', '\t$LIN::Motor = 1;', '}', '', '']
extracted = {}

for line in can_file_content:
    if "$LIN" in line:  # extract the relevant line
        parsed_line = line.replace(";", "").replace("\t", "")  # remove ";" and "\t"
        variable, value = parsed_line.split("=")  # split on "="
        extracted[variable.strip()] = value.strip()  # remove whitespaces

output is {'$LIN::Kl_15': '1', '$LIN::Motor': '1'} output 是{'$LIN::Kl_15': '1', '$LIN::Motor': '1'}
now you can access your new variables with extracted['$LIN::Motor'] which is 1现在您可以使用extracted['$LIN::Motor']访问新变量,即1

暂无
暂无

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

相关问题 如何提取文本文件中的特定行 - How to extract specific line in text file 如何从文本文件中删除特定行? - How do I remove a specific line out of a text file? 如何在 Python 中的文本中提取行的特定部分 - How to extract specific part of a line in a text in Python 如何从文本文件中提取特定文本 - How to extract specific text from text file 如何从文本文件中提取特定行,然后从这些提取的行中提取括号之间的值并将它们放在另一个文件中 - How to extract specific lines from a text file and then from these extracted line, extract the values between parantheses and put them in another file 如何从没有库仑标题的文本文件中将一个特定的列提取到熊猫数据帧 - how to extract one specific column to a panda data frame from a text file with out coulmn headers 如何从python中的文本文件的多行提取两个特定数字 - How can I extract two specific numbers from multiple line of a text file in python 如何编写代码以使用python从pdf文件中提取同一行上的特定文本和整数? - how to write code to extract a specific text and integer on the same line from a pdf file using python? 如何从文本文件min,max,median中提取特定行信息并删除所选行? - How to Extract specific row information from text file min,max,median and to delete a chosen line? 读取 python 中的文本文件并在每一行中提取特定值? - read text file in python and extract specific value in each line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM