|
|
# -*- coding: utf-8 -*-
|
|
|
import os
|
|
|
import re
|
|
|
|
|
|
import matplotlib
|
|
|
import pandas as pd
|
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
|
import milkSpider
|
|
|
import settings
|
|
|
|
|
|
plt.rcParams['font.family'] = settings.FONT
|
|
|
# pd.set_option('display.expand_frame_repr', True)
|
|
|
# pd.set_option('display.max_colwidth', 10)
|
|
|
|
|
|
class view:
|
|
|
|
|
|
def __init__(self, itemList):
|
|
|
self.name = itemList[1]
|
|
|
self.string = itemList[10]
|
|
|
|
|
|
def getFont(): # 列出可用的字体
|
|
|
font = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
|
|
|
for i in font:
|
|
|
print(i)
|
|
|
|
|
|
def main(self):
|
|
|
|
|
|
def str2data(string) -> list:
|
|
|
reg = r"[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)"
|
|
|
pattern = re.compile(reg)
|
|
|
return pattern.findall(string)
|
|
|
|
|
|
def show(itemList):
|
|
|
|
|
|
x = []
|
|
|
y = []
|
|
|
itemList.pop()
|
|
|
itemList.reverse()
|
|
|
|
|
|
for temp in itemList:
|
|
|
|
|
|
date = temp[0] + "月" + temp[1] + "日"
|
|
|
price = "¥" + temp[2] + "元"
|
|
|
x.append(date)
|
|
|
y.append(price)
|
|
|
|
|
|
date = temp[3] + "月" + temp[4] + "日"
|
|
|
price = "¥" + temp[5] + "元"
|
|
|
x.append(date)
|
|
|
y.append(price)
|
|
|
|
|
|
plt.title("商品 [{}] 价格趋势".format(self.name))
|
|
|
plt.bar(x, y, color = 'g', align = 'center')
|
|
|
plt.xticks(size = 10.0, rotation = 45)
|
|
|
plt.xlabel("日期")
|
|
|
plt.ylabel("价格")
|
|
|
plt.plot(x, y, color = 'red', linewidth = 5.0, linestyle = '--')
|
|
|
|
|
|
print("等待可视化界面结束。。。")
|
|
|
plt.show()
|
|
|
|
|
|
if self.string == 0:
|
|
|
print("该商品历史价格趋势数据尚未被收录!")
|
|
|
return
|
|
|
itemList = []
|
|
|
print("以下是商品 [{}] 的历史价格趋势:".format(self.name))
|
|
|
for astr in self.string.split(';'):
|
|
|
strList = str2data(astr)
|
|
|
try:
|
|
|
print(astr)
|
|
|
itemList.append(strList)
|
|
|
except BaseException:
|
|
|
break
|
|
|
show(itemList)
|
|
|
|
|
|
def listCatalogues(): # 列出缓存目录下的条目
|
|
|
path = r"./Catalogues/"
|
|
|
if not os.path.exists(path):
|
|
|
print("当前目录[{}]下没有数据!".format(path))
|
|
|
return False, None, None
|
|
|
if len(os.listdir(path)) is 0:
|
|
|
print("当前目录[{}]下没有数据!".format(path))
|
|
|
return False, None, None
|
|
|
dirList = os.listdir(path)
|
|
|
fileList = []
|
|
|
for filename in dirList:
|
|
|
fileList.append(path + filename)
|
|
|
return True, len(fileList), fileList
|
|
|
|
|
|
def getData(filename, catalogue): # 选择读取数据的方式
|
|
|
while True:
|
|
|
print("# 当前选择的目录是 {} .".format(catalogue))
|
|
|
milkSpider.showBanner(menu = "view")
|
|
|
print("选择一项以查看or返回:", end = '')
|
|
|
choice = str(input())
|
|
|
case = {'1': '评论数量(条)', '2': '价格(人民币)'}
|
|
|
if choice in case.keys():
|
|
|
mode = case.get(choice)
|
|
|
sort = False
|
|
|
break
|
|
|
elif choice == '3': return
|
|
|
else: print("无效选择!")
|
|
|
|
|
|
while True:
|
|
|
print("当前选择的模式是以 [{}] 为基准的排序方式".format(mode))
|
|
|
print("想要查看多少条数据(int):", end = '')
|
|
|
try:
|
|
|
flag = eval(input())
|
|
|
break
|
|
|
except BaseException:
|
|
|
print("无效输入!")
|
|
|
continue
|
|
|
|
|
|
df = pd.read_csv(filename, encoding = 'utf-8', header = 0, error_bad_lines = False)
|
|
|
if choice == '2': sort = True
|
|
|
dfnew = df.sort_values(by = mode, ascending = sort)
|
|
|
|
|
|
try:
|
|
|
dfnew = dfnew.fillna(0)
|
|
|
dfnew[["评论数量(条)"]] = dfnew[["评论数量(条)"]].astype(int)
|
|
|
except BaseException:
|
|
|
pass
|
|
|
|
|
|
dfnew = dfnew[:flag]
|
|
|
while True:
|
|
|
print(dfnew.iloc[:, [1, 2, 3]])
|
|
|
indexList = list(dfnew.index)
|
|
|
while True:
|
|
|
print("选择一项id以查看详细信息(或者 [r] 返回上层目录):", end = '')
|
|
|
try:
|
|
|
index = str(input())
|
|
|
index = eval(index)
|
|
|
if index in indexList:
|
|
|
break
|
|
|
else:
|
|
|
print("无效输入!")
|
|
|
continue
|
|
|
except BaseException:
|
|
|
if index == 'r': return
|
|
|
print("无效输入!")
|
|
|
continue
|
|
|
toShow = dfnew.loc[index]
|
|
|
print(toShow)
|
|
|
aitem = view(toShow)
|
|
|
aitem.main()
|
|
|
while True:
|
|
|
flag = str(input("输入 [r]返回上一级菜单 [c]继续查看: "))
|
|
|
if flag == 'r': return
|
|
|
elif flag == 'c': break
|
|
|
else: print("无效选项!")
|
|
|
|
|
|
def main():
|
|
|
check, length, fileList = listCatalogues()
|
|
|
while check:
|
|
|
print("检测到当前缓存中共有{}个目录:".format(length))
|
|
|
case = {}
|
|
|
for i in range(length):
|
|
|
print("# {}.{}".format(i + 1, fileList[i][13:-4]))
|
|
|
case[str(i+1)] = fileList[i]
|
|
|
print("# {}.输入 [r] 返回上一级菜单".format(i + 2))
|
|
|
print("选择一项以查看or返回:", end = '')
|
|
|
choice = str(input())
|
|
|
if choice in case.keys():
|
|
|
getData(case.get(choice), case.get(choice)[13:-4])
|
|
|
elif choice == 'r': return
|
|
|
else: print("无效选择!")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
# fileList = listCatalogues()
|
|
|
# length, dataList = getData(fileList)
|
|
|
main()
|
|
|
|
|
|
|
|
|
'''
|
|
|
# 数据调试
|
|
|
import pandas as pd
|
|
|
|
|
|
filename = "./Catalogues/milk.csv"
|
|
|
df = pd.read_csv(filename, encoding = 'utf-8', header = 0, error_bad_lines = False)
|
|
|
|
|
|
|
|
|
'''
|