[python]处理文件并排序

本文针对一道笔试题目进行了详细解答,该题目要求从指定文件中筛选包含特定URL的数据行,并按数值排序后输出前三条记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【这是博主去年写的一直扔在草稿箱的文章...都不太有印象了(是我写的么?)...快来检查错误....】

笔试的时候,碰到一个题目,大概意思是有一个文件,比如下面这样的:

Log.txt

挑出带 '/bookmark/' 的URL,并且按照第二列的数字大小进行排序,取最大的三行数据

40:01:67:77 623   http://www.baidu.com/bookmark/0  2013-10-20-21-05-140
28:59:64:31 461   http://www.baidu.com/bookmark/1  2013-10-20-21-05-141
74:20:88:31 767   http://www.baidu.com/bookmark/2  2013-10-20-21-05-142
76:37:77:23 936   http://www.baidu.com/bookmark/3  2013-10-20-21-05-143
20:83:92:84 494   http://www.baidu.com/bookmark/4  2013-10-20-21-05-144
95:73:49:72 780   http://www.baidu.com/bookmark/5  2013-10-20-21-05-145
68:31:23:39 479   http://www.baidu.com/bookmark/6  2013-10-20-21-05-146
04:02:88:15 583   http://www.baidu.com/bookmark/7  2013-10-20-21-05-147
76:18:41:12 438   http://www.baidu.com/bookmark/8  2013-10-20-21-05-148
91:26:32:15 790   http://www.baidu.com/bookmark/9  2013-10-20-21-05-149
34:80:26:32 452   http://www.baidu.com/bookstore/0  2013-10-20-21-05-140
53:40:23:66 223   http://www.baidu.com/bookstore/1  2013-10-20-21-05-141
11:94:98:34 766   http://www.baidu.com/bookstore/2  2013-10-20-21-05-142
05:97:33:78 263   http://www.baidu.com/bookstore/3  2013-10-20-21-05-143
64:37:79:73 746   http://www.baidu.com/bookstore/4  2013-10-20-21-05-144

用下面的代码生成上面的txt文件做测试:

import time
import os
import random

上面是会用到的那啥,下面是生成文件代码:

f = open('Log.txt','w')
for each in range(10):
    f.write(str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+' ')
    f.write(str(random.randint(111,999))+'   ')
    f.write("http://www.baidu.com/bookmark/"+str(each)+'  ')
    f.write(time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime())+str(each)+'\n')
for each in range(5):
    f.write(str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+' ')
    f.write(str(random.randint(1111111111,9999999999))+'   ')
    f.write("http://www.baidu.com/bookstore/"+str(each)+'  ')
    f.write(time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime())+str(each)+'\n')
f.close()

窝是技术渣,只好这种死蠢的方法生成文件QAQ

咳,接下去开始处理数据:

results=[]
i=0
fp=open('Log.txt','r')
for each in fp.readlines():
    if('/bookmark/' in each):
        results.append(tuple(each.split()))
max=sorted(results,key=lambda result:result[1],reverse=True)
print(max[0:2])
fp.close()

嗯,看看结果:

[('76:37:77:23', '936', 'http://www.baidu.com/bookmark/3', '2013-10-20-21-05-143'), ('91:26:32:15', '790', 'http://www.baidu.com/bookmark/9', '2013-10-20-21-05-149'), ('95:73:49:72', '780', 'http://www.baidu.com/bookmark/5', '2013-10-20-21-05-145')]

哟西,Top 3粗来了~\(≧▽≦)/~

=。=因为笔试的时候没做出来,很不甘心……

感谢各种bo主的经验总结>.<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值