You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import csv
import re
from collections import Counter
# 正则表达式匹配时间
time_pattern = re . compile ( r ' \ d {4} - \ d {2} ' )
# 初始化计数器
date_counts = Counter ( )
# 打开并读取CSV文件
with open ( ' congtent.csv ' , mode = ' r ' , encoding = ' utf-8 ' ) as file :
csv_reader = csv . reader ( file )
# print(csv_reader)
# header = next(csv_reader) # 跳过标题行
for row in csv_reader :
# print(row)
time = row [ 14 ]
# print("time", time)
# 搜索时间值
time_match = time_pattern . search ( time )
if time_match :
# print(f"Found time: {time_match.group()}")
date = time_match . group ( )
date_counts [ date ] + = 1
# 打印每一天的时间出现次数
for date , count in date_counts . items ( ) :
print ( f " { date } : { count } times " )
# 可视化展示
dates = list ( date_counts . keys ( ) )
counts = list ( date_counts . values ( ) )
print ( dates )
print ( counts )
from pyecharts . charts import Bar
from pyecharts import options as opts
# 创建柱状图
bar = (
Bar ( )
. add_xaxis ( dates )
. add_yaxis ( " 数量 " , counts )
. set_series_opts ( itemstyle_opts = { ' color ' : ' blue ' } )
. set_global_opts ( title_opts = opts . TitleOpts ( title = " 日期对应数量柱状图 " ) )
)
# 生成html文件( 可选)
bar . render ( " bar_chart.html " )