简体   繁体   English

使用python读取文本文件并按扩展名排序

[英]Reading a text file and sorting by extension using python

I would like to read a text file using python.我想使用 python 读取文本文件。 If a line with .cpp found, process it first and then if a line with .java extension found, process it second .如果找到带有 .cpp 的行,首先处理它,然后如果找到带有 .java 扩展名的行,则第二次处理它。

Test.py is my sample code, using a for loop, it will read Test.txt and then it will process COMPILE_JAVA() method first and then it will execute COMPILE_CPP() method. Test.py 是我的示例代码,使用 for 循环,它会读取 Test.txt 然后它会先处理 COMPILE_JAVA() 方法,然后它会执行 COMPILE_CPP() 方法。

Test.txt测试.txt

/home/jenkins/workspace/a/Hello.java
/home/jenkins/workspace/b/Hello.cpp
/home/jenkins/workspace/b/Hello1.cpp

Test.py测试文件

for f in files:
    ACTION1 = False
    ACTION2 = False
    with open(f, 'r') as file:
        for line in (file):
            if ACTION1 is False and ('.cpp' in line ):
                COMPILE_CPP()
                ACTION1 = True
            elif ACTION2 is False and '.java' in line:
                COMPILE_JAVA()
                ACTION2 = True
                break

IIUC you can sort the list you read from test.txt by extension using os.path.splitext : IIUC 您可以使用os.path.splitext按扩展名对从test.txt读取的列表进行排序:

import os

with open(f, 'r') as file:
    sorted_files = sorted(file, key=lambda x: os.path.splitext(x)[1])
    for line in sorted_files:
        # rest of the code

Given给定的

l = ['/home/jenkins/workspace/a/Hello.java',
     '/home/jenkins/workspace/b/Hello.cpp',
     '/home/jenkins/workspace/b/Hello1.cpp']

Then running sorted(l, key=lambda x: os.path.splitext(x)[1]) returns:然后运行sorted(l, key=lambda x: os.path.splitext(x)[1])返回:

['/home/jenkins/workspace/b/Hello.cpp',
 '/home/jenkins/workspace/b/Hello1.cpp',
 '/home/jenkins/workspace/a/Hello.java']

Explanation:解释:

os.path.splitext returns a tuple (root, ext), eg os.path.splitext("dir/myfile.txt") returns ("dir/myfile", ".txt") , so lambda x: os.path.splitext(x)[1] returns the second part ( .txt ). os.path.splitext返回一个元组 (root, ext),例如os.path.splitext("dir/myfile.txt")返回("dir/myfile", ".txt") ,所以lambda x: os.path.splitext(x)[1]返回第二部分 ( .txt )。

This is then passed as an argument to the sorted function, so the list gets sorted alphabetically by extension.然后将其作为参数传递给sorted函数,因此列表按扩展名按字母顺序排序。

Maybe this can help?也许这可以帮助? I used bubble sort to sort the extensions.我使用冒泡排序对扩展进行排序。

row_list = []
with open("test.txt", "r") as file:
    line = file.readline()
    while line:
        row_list.append(line)

for i in range(1, len(row_list)):
    for j in range(len(row_list), i, -1):
        ext1 = row_list[j-1].split(".")[1]
        ext2 = row_list[j].split(".")[1]
        if ext1 > ext2:
            row_list[j-1], row_list[j] = row_list[j], row_list[j-1]

with open("output_file.txt", "w") as file:
    for row in row_list:
        file.write(f"{row}\n")

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

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