正在 python3 中排序文本文件

Sorting text file in python3

我有一个文本文件 bk.txt 如下所示:

file 'wave bass 1.aif' 
file 'wave bass 10.aif' 
file 'wave bass 11.aif' 
file 'wave bass 12.aif' 
file 'wave bass 13.aif' 
file 'wave bass 14.aif' 
file 'wave bass 15.aif' 
file 'wave bass 16.aif' 
file 'wave bass 2.aif' 
file 'wave bass 3.aif' 
file 'wave bass 4.aif' 
file 'wave bass 5.aif' 
file 'wave bass 6.aif' 
file 'wave bass 7.aif' 
file 'wave bass 8.aif' 
file 'wave bass 9.aif' 
file 'wave effect 1.aif' 
file 'wave effect 2.aif' 
file 'wave effect 3.aif' 
file 'wave effect 4.aif' 
file 'wave effect 5.aif' 
file 'wave effect 6.aif' 
file 'wave effect 7.aif' 
file 'wave effect 8.aif' 
file 'wave hit 1.aif' 
file 'wave hit 10.aif' 

我正在尝试按字母顺序逐行对文件进行排序并将结果保存到新文件中,我尝试了几种不同的排序方法,如下所示:

        fn = songsFilepath + "bk.txt"
        sorted_fn = songsFilepath + "bkSorted.txt"

        with open(fn,'r') as first_file:
            rows = first_file.readlines()
            sorted_rows = sorted(rows, key=lambda x: int(x.split()[0]), reverse=False)
            with open(sorted_fn,'w') as second_file:
                for row in sorted_rows:
                    second_file.write(row)

但是每当我 运行 代码时,我都会收到一个错误,谈论 int(0 with base 10 in 'file'

的无效文字
Traceback (most recent call last):
  File "commandLineMusicVideo/__init__.py", line 213, in <module>
    fullAlbum(songsFilepath, audioFormat, imageFilepath, outputResolution)
  File "commandLineMusicVideo/__init__.py", line 87, in fullAlbum
    sorted_rows = sorted(rows, key=lambda x: int(x.split()[0]), reverse=False)
  File "commandLineMusicVideo/__init__.py", line 87, in <lambda>
    sorted_rows = sorted(rows, key=lambda x: int(x.split()[0]), reverse=False)
ValueError: invalid literal for int() with base 10: 'file'

我知道这是一个不太常见的文本文件,但我的排序代码是否有问题导致文件无法正确读取/排序到新文件中?

谢谢

编辑编辑编辑

正在处理 Patrick 提出的修复,当前 python 代码是:

        fn = songsFilepath + "bk.txt"
        sorted_fn = songsFilepath + "bkSorted.txt"

        with open(fn,'r') as first_file:
            rows = first_file.readlines()
        
        #sort 1
        sorted_rows = sorted(rows, key=lambda x: (x.strip().split()[:3],int(x.strip().split()[-1].split(".")[0])), reverse=False)

        #sort 2, change the sorting function to take the last split and split before the .
        sorted_rows = sorted(rows, key = lambda x: int(x.strip().split()[-1].split(".")[0]), reverse = False)

        #write results to file
        with open(sorted_fn,'w') as second_file:
            for row in sorted_rows:
                second_file.write(row)

        print('finished sorted = ')
        print(open(sorted_fn).read())

我正在尝试实现获得最佳排序输出所需的决斗排序,但目前正在查看输出排序:

file 'wave bass 1.aif'     
file 'wave effect 1.aif'   
file 'wave hit 1.aif'      
file 'wave tone 1.aif'     
file 'wave bass 2.aif'     
file 'wave effect 2.aif'   
file 'wave tone 2.aif'     
file 'wave bass 3.aif'     
file 'wave effect 3.aif'   
file 'wave hit 3.aif'      
file 'wave tone 3.aif'     
file 'wave bass 4.aif'     
file 'wave effect 4.aif'   
file 'wave hit 4.aif'      
file 'wave tone 4.aif'     ```

你的文件的一行是

file 'name number.aif'

您尝试通过拆分并取第一部分来排序:

int("file 'wave bass 10.aif'\n".split()[0])  => 'file' - not a number ->int() crashes

修复:

with open ("bk.txt","w") as f:f.write("""file 'wave bass 1.aif'
file 'wave bass 10.aif'\nfile 'wave bass 11.aif'\nfile 'wave bass 12.aif'
file 'wave bass 13.aif'\nfile 'wave bass 14.aif'\nfile 'wave bass 15.aif'
file 'wave bass 16.aif'\nfile 'wave bass 2.aif'\nfile 'wave bass 3.aif'
file 'wave bass 4.aif'\nfile 'wave bass 5.aif'\nfile 'wave bass 6.aif'
file 'wave bass 7.aif'\nfile 'wave bass 8.aif'\nfile 'wave bass 9.aif'
file 'wave effect 1.aif'\nfile 'wave effect 2.aif'\nfile 'wave effect 3.aif'
file 'wave effect 4.aif'\nfile 'wave effect 5.aif'\nfile 'wave effect 6.aif'
file 'wave effect 7.aif'\nfile 'wave effect 8.aif'\nfile 'wave hit 1.aif'
file 'wave hit 10.aif'
""")

fn = "bk.txt"
sorted_fn = "bkSorted.txt"

with open(fn,'r') as first_file:
    rows = first_file.readlines()

# change the sorting function to take the last split and split before the .
sorted_rows = sorted(rows, key = lambda x: int(x.strip().split()[-1].split(".")[0]), 
                     reverse = False)
with open(sorted_fn,'w') as second_file:
    for row in sorted_rows:
        second_file.write(row)

print(open(sorted_fn).read())

输出:

file 'wave bass 1.aif'
file 'wave effect 1.aif'
file 'wave hit 1.aif'
file 'wave bass 2.aif'
file 'wave effect 2.aif'
file 'wave bass 3.aif'
file 'wave effect 3.aif'
file 'wave bass 4.aif'
file 'wave effect 4.aif'
file 'wave bass 5.aif'
file 'wave effect 5.aif'
file 'wave bass 6.aif'
file 'wave effect 6.aif'
file 'wave bass 7.aif'
file 'wave effect 7.aif'
file 'wave bass 8.aif'
file 'wave effect 8.aif'
file 'wave bass 9.aif'
file 'wave bass 10.aif'
file 'wave hit 10.aif'
file 'wave bass 11.aif'
file 'wave bass 12.aif'
file 'wave bass 13.aif'
file 'wave bass 14.aif'
file 'wave bass 15.aif'
file 'wave bass 16.aif'

要保留在 "sections" 内,您需要进行两部分排序:

key=lambda x: (x.strip().split()[:3],   # sort by first textual parts
               int(x.strip().split()[-1].split(".")[0])), reverse=False) # then number

获得

file 'wave bass 1.aif'
file 'wave bass 2.aif'
file 'wave bass 3.aif'
file 'wave bass 4.aif'
file 'wave bass 5.aif'
file 'wave bass 6.aif'
file 'wave bass 7.aif'
file 'wave bass 8.aif'
file 'wave bass 9.aif'
file 'wave bass 10.aif'
file 'wave bass 11.aif'
file 'wave bass 12.aif'
file 'wave bass 13.aif'
file 'wave bass 14.aif'
file 'wave bass 15.aif'
file 'wave bass 16.aif'
file 'wave effect 1.aif'
file 'wave effect 2.aif'
file 'wave effect 3.aif'
file 'wave effect 4.aif'
file 'wave effect 5.aif'
file 'wave effect 6.aif'
file 'wave effect 7.aif'
file 'wave effect 8.aif'
file 'wave hit 1.aif'
file 'wave hit 10.aif'