下面是我正在使用的代码,尽管控制台输出中没有错误,但仍然不返回任何内容。为什么代码没有输出?有什么我需要更改或修复的。我想我正确导入了扫描仪,但是可能存在问题。谢谢!
import sys
from scanner import Scanner
from functools import cmp_to_key
class Tweet:
def __init__(self, tweeter, tweet, time):
self.tweeter = tweeter[1:]
self.tweet = tweet
self.time = time
def __str__(self):
return self.tweeter+" "+self.time
def display(self):
return self.tweeter+" " +self.tweet
def create_record(s):
tweets = []
tweeter = s.readtoken()
tweet1count = 0
while tweeter != "":
tweet = s.readstring()
t1 = Tweet(tweeter, tweet, s.readline())
tweet1count += 1
tweeter = s.readtoken()
tweets.append(t1)
return tweets
def read_records(file):
s = Scanner(file)
return create_record(s)
def is_more_recent(t1, t2):
year, month, day, time = t1.time.split()
month = ("0" + month)[-2:]
day = ("0" + day)[-2:]
timestamp1 = year + month + day + time
year, month, day, time = t2.time.split()
month = ("0" + day)[-2:]
day = ("0" + day)[-2:]
timestamp2 = year + month + day + time
return timestamp1 > timestamp2
def merge_and_sort_tweets(tweets1, tweets2):
tweets = tweets1 + tweets2
cmp_items_py3 = cmp_to_key(is_more_recent)
tweets.sort(key = cmp_items_py3)
return tweets
def write_records(file, tweets):
file = open(file, "w+")
for t in tweets:
file.write(str(t))
def main():
print("Reading Files")
tweets1 = read_records(sys.argv[1])
tweets2 = read_records(sys.argv[2])
tweet1count = len(tweets1)
tweet2count = len(tweets2)
if tweet1count > tweet2count:
print("tweet1.txt contained the most tweets with" , tweet1count)
elif tweet1count < tweet2count:
print("tweet2.txt contained the most tweets with" , tweet2count)
else:
print("tweet1.txt contains ", tweet1count, "tweets. ")
print("tweet2.txt contains ", tweet2count, "tweets. ")
print("Merging files...")
tweets = merge_and_sort_tweets(tweets1, tweets2)
print("Writing file...")
write_records(sys.argv[3], tweets)
print("File writtem. Displying", min(5, len(tweets)),"earliest tweeters and tweets.")
for i in range(min(5, len(tweets))):
print(tweets[i].display())
if __name__ == "__main__":
main()