我使用我编写的程序合并了两个 .txt 文件。现在,一旦我将这两个 .txt 文件组合在一起,我就会得到未排序的数据组合。
我的数据看起来像这样
Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1
Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2
但是我希望它看起来像这样
['Bud', 'Abbott', 51, 92.3]
['Don', 'Adams', 51, 90.4]
['Mary', 'Boyd', 52, 91.4]
['Jill', 'Carney', 53, 76.3]
['Hillary', 'Clinton', 50, 82.1]
['Randy', 'Newman', 50, 41.2]
我如何将我拥有的变成我想要的
顺便说一句,我必须得到我的第一组数据的代码看起来像这样
def ReadAndMerge():
library1=input("Enter 1st filename to read and merge:")
with open(library1, 'r') as library1names:
library1contents = library1names.read()
library2=input("Enter 2nd filename to read and merge:")
with open(library2, 'r') as library2names:
library2contents = library2names.read()
print(library1contents)
print(library2contents)
combined_contents = library1contents + library2contents # concatenate text
file = combined_contents
lines = []
for l in file:
line = l.strip().split(" ")
lines.append(line)
lines.sort(key=lambda lines: lines[1])
for line in lines:
print(line)
print(combined_contents)
return(combined_contents)
读取并合并()
库 1 和库 2 分别是这样的
Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1
Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2
我如何对其进行排序并在列表中
我用你的新行更新了我的程序,但是当我运行它时,我不断收到这个错误
Traceback (most recent call last):
File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 30, in <module>
ReadAndMerge()
File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 23, in ReadAndMerge
lines.sort(key=lambda lines: lines[1])
File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 23, in <lambda>
lines.sort(key=lambda lines: lines[1])
IndexError: list index out of range
>>>