[英]Read from file, Sort list by revenue, Write to file
我收到了一个包含 500 部电影的文本文件,格式如下:“名称”\t“总收入”
我必须编写一个 function 按总收入对电影进行排序,然后将排序后的内容写入目标文件。
我被困在多个计数上,出现的错误消息是这样的: 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!")
您正在删除 2 元素列表的第一个元素,然后尝试访问现在 1 元素列表的第二个元素。 删除split.pop()
或将split[1] = int(split[1])
中的索引替换为0
。
在 bash 中,您可以通过以下方式轻松实现:
cat top500.txt | sort -n -k 2 > top500result.txt
如果文件中只有两列,则在读取一行时会得到一个 2 元素列表。 由于“split”在每次迭代中都是一个 2 元素列表,因此 pop() 将删除最后一个元素,导致列表最后只有 1 个元素。 使用 1 索引“拆分”列表将无效,因为“拆分”现在是一个 1 元素列表,并且 python 从 0 开始索引列表。
我知道这不是最优雅的解决方案,但是嘿..这行得通。 问题在于您的正则表达式没有给您所需的格式。
您可以尝试以下解决方案:
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!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.