浏览目录,然后在专门命名的子目录中找到一个文本文件,然后将数据提取到列表

发布于 2025-01-23 12:23:08 字数 1904 浏览 1 评论 0原文

我有一个名为“ scratch”的父目录,在那个父级,我有几个带有随机名称的文件夹,然后在这些文件夹中,有一个名为“ deagg''的文件夹,然后在“ deagg”中有一个随机文件夹名称,然后在该文件夹中我想解析一个“ summary.txt”文件以获取数字。如果“ summary.txt”文件中的一行说“平板”,则提取该行中的浮点数,然后将其添加到列表slab_sum中。

我的问题是下面的小型代码。我获得将“平板”总计附加到列表的唯一方法是检测目录的更改。我如何将每个目录/summary.txt的slab_total添加到slab_total到slab_sum列表,而无需使用下面的小代码来检测目录更改。我希望它可以通过summary.txt文件解析,然后将其发送到slab_sum列表后。另外,我有四个...不仅是平板,我对这个示例简单,因此在每个summary.txt中搜索了四个不同的关键词。

if prev_dir is None:
    prev_dir = root
elif prev_dir != root:
    slab_sum.append(str(slab_total))
    prev_dir = root

示例目录:'c://scratch//pga//deagg//apple/summary.txt'

File = 'summary.txt'
Start_Path = 'C://scratch'
slab_sum = []

def extract_data():
    prev_dir = None
    for root, dirs, files in os.walk(Start_Path):
        if File in files and root.split(os.path.sep)[1] == "deagg":
            summary_path = root + os.path.sep + File
            search_file = open(summary_path, 'r', encoding='utf-8')
            slab_count = 0
            slab_number = []
            for line in search_file:
            #for i, line in enumerate(search_file):
                # Parse through 'summary.txt' for any line that has 'Slab', record only the end float number.
                # Convert float to decimal rounded to hundredth place
                if "Slab" in line:
                    slab_num = ([float(s) for s in re.findall(r'[-+]?(?:\d*\.\d+|\d+)', line)])
                    slab_number.append(slab_num)
                    slab_count = slab_count + 1
                    slab_total = 0
                    for slab_num in slab_number:
                        slab_total += slab_num
                if prev_dir is None:
                    prev_dir = root
                elif prev_dir != root:
                    slab_sum.append(str(slab_total))
                    prev_dir = root
            search_file.close()

I have a parent directory named 'scratch', within that parent dir I have several folders with random names, then within those folders there is a folder named 'deagg', then within 'deagg' there is a random folder name, then within that there is a 'summary.txt' file that I want to parse through to get numbers out of. If a line in the 'summary.txt' file says 'Slab' this extracts the float number in that line, then adds it to the list slab_sum.

My issue is with the small code below. The only way I could get it to append the total of 'slab' to the list was to detect a change in directory. How can I have this append the slab_total to slab_sum list for each directory/summary.txt without using the small code below to detect directory change. I want it to parse through the summary.txt file, then once it does that send it to the slab_sum list. Also, I have four of these...not just slab, I made it simple for this example, so there are four different key words I search for in each summary.txt.

if prev_dir is None:
    prev_dir = root
elif prev_dir != root:
    slab_sum.append(str(slab_total))
    prev_dir = root

Example directory: 'C://scratch//PGA//deagg//apple/summary.txt'

File = 'summary.txt'
Start_Path = 'C://scratch'
slab_sum = []

def extract_data():
    prev_dir = None
    for root, dirs, files in os.walk(Start_Path):
        if File in files and root.split(os.path.sep)[1] == "deagg":
            summary_path = root + os.path.sep + File
            search_file = open(summary_path, 'r', encoding='utf-8')
            slab_count = 0
            slab_number = []
            for line in search_file:
            #for i, line in enumerate(search_file):
                # Parse through 'summary.txt' for any line that has 'Slab', record only the end float number.
                # Convert float to decimal rounded to hundredth place
                if "Slab" in line:
                    slab_num = ([float(s) for s in re.findall(r'[-+]?(?:\d*\.\d+|\d+)', line)])
                    slab_number.append(slab_num)
                    slab_count = slab_count + 1
                    slab_total = 0
                    for slab_num in slab_number:
                        slab_total += slab_num
                if prev_dir is None:
                    prev_dir = root
                elif prev_dir != root:
                    slab_sum.append(str(slab_total))
                    prev_dir = root
            search_file.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文