Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:对文件进行排序_Python_Sorting - Fatal编程技术网

Python:对文件进行排序

Python:对文件进行排序,python,sorting,Python,Sorting,我有一个twitter日志的数据,我必须对文件进行排序以显示 每个用户的转发推文排名 这是密码 import codecs with codecs.open('hoge_qdata.tsv','r', 'utf-8') as tweets: tweet_list = tweets.readlines() tweet_list.pop(0) facul={} for t in tweet_list: t=t.split('\t') if not t[0] in fac

我有一个twitter日志的数据,我必须对文件进行排序以显示 每个用户的转发推文排名

这是密码

import codecs

with codecs.open('hoge_qdata.tsv','r', 'utf-8') as tweets:
    tweet_list = tweets.readlines()

tweet_list.pop(0)

facul={}
for t in tweet_list:
    t=t.split('\t')
    if not t[0] in facul:
        facul[t[0]]=[]
    facul[t[0]].append(t)
    t[-2]= int(t[-2])

def cmp_retweet(a,b):
    if a[-2]<b[-2]:
        return 1
    if a[-2]>b[-2]:
        return -1
    return 0

for f in sorted(facul.keys()):
    facul[f].sort(cmp = cmp_retweet)
    print ('[%s]' %(f))
    for t in facul[f][:5]:
        print ('%d:%s:%s' %(t[-2], t[2], t[-1].strip()))
但这就是我得到的

[jin_nkzw]
325:46936947935035392:RT @shimshamshimmy: SFCの学生と教職員の方へ、安否確認に協力してください。SFC-SFSで伝えてください。 https://vu8.sfc.keio.ac.jp/sfc-sfs/
0:8356641661:麹町のNICTで会議。ビルの目の前に、今朝のテレビでやってた「ふゆざくら」が咲いていた。
0:4979124091:@sfc_orf リンゴの木は会場俯瞰図(断面)からタッチパネルが生えているということで、「断面の触覚」という名前が浮かびました。「断面の触感」と言葉が近すぎて誤植と思われますかね〜
0:11422290558:いまになって、@who_meさんをフォローしていなかったことに気づく
0:7940726154:秋葉原で1000円程度で手配できるおいしいお弁当といったら何でしょう?
格式是。。 转发次数:id:content


我不知道如何删除这些tweet。

我想这会起作用:

for t in tweet_list:
    t=t.split('\t')
    t[-2]= int(t[-2])   # Move this up here.
    if t[-2] <= 0:      # Check the retweet value.
        continue        # If it's 0 or less, skip this tweet completely.
    if not t[0] in facul:
        facul[t[0]]=[]
    facul[t[0]].append(t)

在打印之前,你能不能检查一下
t[-2]==0
?一个有用的提示:不要在d:d[k]=[]中使用
if not k这样的结构,而是使用。不知怎的,我在打印“%d:%s:%s%”(t[-2],t[2],t[-1])中的第29行说文件“kadai3-2.py时出现了错误,在打印“%d:%s%”(t[-2],t[2],t[-1])。编码('utf-8').strip())UnicodeDecodeError:“ascii”编解码器无法解码第19位的字节0xe4:序号不在范围内(128)这里发生了什么??如何解决此问题?您试图打印的推文中似乎有一条包含无法解码的字符。我尝试在facul[f][:5]中使用t:打印“%d:%s:%s%”(t[-2],t[2],t[-1]。encode('utf-8').strip()),但无法工作。。。有没有其他方法可以解决这个问题?如果没有看到你想要打印的字符,很难说。这可能是一个完全不同的问题。
for t in tweet_list:
    t=t.split('\t')
    t[-2]= int(t[-2])   # Move this up here.
    if t[-2] <= 0:      # Check the retweet value.
        continue        # If it's 0 or less, skip this tweet completely.
    if not t[0] in facul:
        facul[t[0]]=[]
    facul[t[0]].append(t)
    for t in facul[f][:5]:
        if t[-2] > 0:
            print ('%d:%s:%s' %(t[-2], t[2], t[-1].strip()))