我有一个文件codereview_stats.txt
,其中包含以下格式的数据。顶部的日期表示数据写入文件的最后日期,并且用于我收集数据的方式。
18-09-2013
dd-mm-yyyy, questions, answers, %answered, users, visitors/day
15-09-2013,9381,15913,88,26972,8466
16-09-2013,9413,15949,88,27015,8466
17-09-2013,9426,15968,88,27041,8871
18-09-2013,9439,15995,88,27082,8934
我正在使用 matlotlib 绘制数据。但在此之前,我需要将日期、问题、答案等分成单独的列表。目前我正在get_data()
如下所示的函数中执行此操作。如您所见,这里有许多列表推导。有没有更简单的方法将数据分成许多列表?
#! python3
from matplotlib import pyplot as plt
from datetime import datetime as dt
def get_data():
with open('codereview_stats.txt') as f:
data = [line for line in f]
data = [line[:-1].split(',') for line in data[3:]]
dates = [dt.strptime(line[0], '%d-%m-%Y') for line in data]
ques = [int(line[1]) for line in data]
ans = [int(line[2]) for line in data]
perc = [int(line[3]) for line in data]
users = [int(line[4]) for line in data]
visits = [int(line[5]) for line in data]
return dates, ques, ans, perc, users, visits
def show_plot(dates, ques, ans, perc, users, visits):
plt.plot_date(dates, visits, '.', linestyle = '-')
plt.grid(True)
plt.setp(plt.xticks()[1], rotation=30)
plt.show()
show_plot(*get_data())