turning data into a list

牧云@^-^@ 提交于 2019-12-13 03:59:55

问题


I have combined two .txt files using a program i wrote. Now once i combined these two .txt files i was left with an unsorted combination of data.

my data looks like this

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

however i want it to look like this

 ['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]

how do i transform what i have into what i want

by the way the code i have to get my first set of data looks like this

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)

ReadAndMerge()

and library 1 and library 2 look like this respectively

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

How do i get it to be sorted and in a list

I updated my program with your new lines, but when i run it i keep getting this error

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
 >>> 

回答1:


def getListOfLinesInFile(file): 
    file = open(file, "r")
    lines = []    
    for l in file:
        line = l.strip().split(" ")
        lines.append(line)
    file.close()
    return lines

To use it in your program, just your ReadAndMerge -function with:

def ReadAndMerge():
    library1 = input("Enter 1st filename to read and merge: ")
    library1_list = getListOfLinesInFile(library1)

    library2 = input("Enter 2nd filename to read and merge: ")
    library2_list = getListOfLinesInFile(library1)

    library_merged = library1_list + library2_list
    library_merged.sort(key=lambda library_merged: library_merged[1])

    print(*library_merged)
    return library_merged


来源:https://stackoverflow.com/questions/13458430/turning-data-into-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!