简体   繁体   中英

Read from file, Sort list by revenue, Write to file

I'm given a text file of 500 films formatted as such: "name" \t "gross revenue"

I've to write a function that sorts the films by gross revenue, then write the sorted content to the destination file.

I'm stuck on multiple counts and the error message that appeared is this: split[1]= int(split[1]) IndexError: list index out of range Command exited with non-zero status 1

import re

def sort_films(source, destination):    
    source= open(source)
    destination = open(destination, "w")
    
    full_list=[]
    for line in source:
        split= re.split("\t|\n", line)
        split.pop()
        split[1]= int(split[1])
        
        full_list.append(split)
        full_list.sort(key= lambda i:i[1], reverse=True)
        
    print(full_list, file=destination)
        
    source.close()
    destination.close()
    
sort_films("top500.txt", "top500result.txt")
print("Done!")

You are removing the first element of a 2-element list, and then trying to access the second element of a now 1 element list. Either remove split.pop() or replace the index in split[1] = int(split[1]) with a 0 .

In bash you can achieve this easily with:

cat top500.txt | sort -n -k 2 > top500result.txt

If you only have two columns in file, you will get a 2-elements list when reading a row. Since "split" is a 2-elements list in each iteration, the pop() will delete the last element causing the list last only 1 element. Indexing "split" list with 1 will be invalid because "split" is now a 1 element list and python start indexing list from 0.

I understand this is not the most elegant solution but hey.. this works. The problem was with your regular expression which did not give you the desired formatting.

you can give the below solution a try:

import re


def sort_films(source, destination):
    source = open(source)
    destination = open(destination, "w")

full_list = []
for line in source:
    line = re.sub("^\"|\"$|\n", "", line)
    split = re.split("\"    \"", line)
    split = list(filter(("").__ne__, split))
    split[1] = int(split[1])

    full_list.append(split)
    full_list.sort(key=lambda i: i[1], reverse=True)

print(full_list, file=destination)

source.close()
destination.close()

sort_films("source_file.txt", "out_file.txt")
print("Done!")

  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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