在第3列筛选出日期为当天相应的日期的行,并将整行数据写入字典中 data_dict = {} for row in sheet.iter_rows(min_row=2, min_col=3, max_col=3): if row[0].value.date() == datetime.datetime.now('%Y/%m/%d').date(): data_dict[row[0].row] = [cell.value for cell in sheet[row[0].row]]修改這段代碼,單元個中日期格式是這種'%Y/%m/%d'
时间: 2024-03-28 10:41:59 浏览: 97
好的,根据你的要求,修改后的代码如下:
```
import datetime
data_dict = {}
today = datetime.datetime.now().strftime('%Y/%m/%d')
for row in sheet.iter_rows(min_row=2, min_col=3, max_col=3):
if row[0].value.strftime('%Y/%m/%d') == today:
data_dict[row[0].row] = [cell.value for cell in sheet[row[0].row]]
```
这样就可以筛选出日期为当天的数据,并将整行数据写入字典中。其中,`today`变量用来存储当前日期,格式为`%Y/%m/%d`,然后在循环中,将单元格中的日期格式化为相同的格式,然后与`today`比较,如果相等,则将整行数据写入字典中。
相关问题
import xlrd def result_write(result,i,j):#result为txt文件,i为比赛项目,j为奖项等级 tmp_str='' tmp_num=0 if award_dict[i][j][0]!=0: result.write(j+'获奖人数为'+str(award_dict[i][j][0])+'人,姓名如下:\n') else: result.write(j+'获奖人数为0人\n') for j in award_dict[i][j][1:]: tmp_str+=j tmp_str+=' ' tmp_num+=1 if tmp_num==10:#每十人写入文件并换行 result.write(tmp_str+'\n') tmp_str='' tmp_num=0 if tmp_num!=0:#向文件写入不满十人的姓名组 result.write(tmp_str+'\n') result.write('\n') award_dict=dict() data= xlrd.open_workbook("C:\\Users\\游雏\\Downloads\\2022蓝桥江西省赛.xls") sheet1 = data.sheet_by_name("Sheet1") col = sheet1.col_values(4) award_class=[] for i in col[1:]:#获取比赛项目列表,首字母统一大写 if 'a'<=i[0]<='z': i=i[0].upper()+i[1:] if i not in award_class: award_class.append(i) for i in award_class:#创建一个嵌套字典,一级字典以比赛项目为键,二级字典以奖项等级为键,值为获奖人姓名列表 award_dict[i]=dict(一等奖=[0],二等奖=[0],三等奖=[0]) for i in range(sheet1.nrows):#按行遍历表格 if i==0:continue row = sheet1.row_values(i) if row[2]=='江西理工大学':#row[2]为学校名 if 'a'<=row[4][0]<='z':#row[4]为比赛项目 row[4]=row[4][0].upper()+row[4][1:] award_dict[row[4]][row[5]][0]+=1#统计某项目某奖项的获奖人数,row[5]为奖项等级 award_dict[row[4]][row[5]].append(row[3])#将获奖者姓名添入一级键为项目二级键为奖项的列表,row[3]为获奖者姓名 award_level=['一等奖','二等奖','三等奖'] result=open("resutl.txt", "w") for i in award_class: result.write(i+'\n') for j in award_level: result_write(result,i,j) result.write('\n') result.close()帮我优化此代码
以下是代码优化建议:
1. 将常量定义为变量。将 award_level 的值定义为变量,这样有利于代码维护和修改。
2. 将重复代码提取为函数。可以将统计获奖人数和将获奖者姓名添入列表的代码提取为一个函数,使得代码更加简洁和易读。
3. 使用 with 语句来处理文件对象。这样可以避免手动关闭文件对象的问题。
4. 使用 f-string 格式化字符串。这样可以使得代码更加简洁和易读。
综上所述,以下是优化后的代码:
```python
import xlrd
def count_award(award_dict, project, level, name):
"""统计某项目某奖项的获奖人数和姓名列表"""
award_dict[project][level][0] += 1
award_dict[project][level].append(name)
def write_result(result, project, level, award_dict):
"""将某项目某奖项的获奖人数和姓名列表写入文件"""
num = award_dict[project][level][0]
if num != 0:
result.write(f"{level}获奖人数为{num}人,姓名如下:\n")
else:
result.write(f"{level}获奖人数为0人\n")
names = award_dict[project][level][1:]
for i in range(0, num, 10):
result.write(" ".join(names[i:i+10]) + "\n")
if num % 10 != 0:
result.write(" ".join(names[num//10*10:]) + "\n")
result.write("\n")
def optimize_code():
award_dict = {}
data = xlrd.open_workbook("C:\\Users\\游雏\\Downloads\\2022蓝桥江西省赛.xls")
sheet1 = data.sheet_by_name("Sheet1")
col = sheet1.col_values(4)
award_class = []
for i in col[1:]:
if 'a' <= i[0] <= 'z':
i = i[0].upper() + i[1:]
if i not in award_class:
award_class.append(i)
for i in award_class:
award_dict[i] = {"一等奖": [0], "二等奖": [0], "三等奖": [0]}
for i in range(sheet1.nrows):
if i == 0:
continue
row = sheet1.row_values(i)
if row[2] == '江西理工大学':
if 'a' <= row[4][0] <= 'z':
row[4] = row[4][0].upper() + row[4][1:]
count_award(award_dict, row[4], row[5], row[3])
award_level = ['一等奖', '二等奖', '三等奖']
with open("result.txt", "w") as result:
for i in award_class:
result.write(f"{i}\n")
for j in award_level:
write_result(result, i, j, award_dict)
```
最后需要注意的是,如果要在输出的字符串中包含花括号 {},需要使用两个花括号来进行转义,例如:"{{}}".
import openpyxl import matplotlib.pyplot as plt movie_dict = {} with open('D:\\pythonProject1\\电影信息.txt', 'r',encoding='utf-8') as f: for line in f.readlines(): line = line.strip() movie_info = line.split(';') movie_name = movie_info[0] directors = movie_info[1].split(',') actors = movie_info[2].split(',') for director in directors: if director not in movie_dict: movie_dict[director] = {'movies': [movie_name], 'actors': {}} else: movie_dict[director]['movies'].append(movie_name) for actor in actors: for director in directors: if actor not in movie_dict[director]['actors']: movie_dict[director]['actors'][actor] = 1 else: movie_dict[director]['actors'][actor] += 1 wb = openpyxl.load_workbook('D:\\pythonProject1\\电影信息统计.xlsx') ws = wb.create_sheet('导演作品统计',0) ws.title = '导演作品统计' ws.cell(row=1, column=1, value='导演姓名') ws.cell(row=1, column=2, value='执导电影数量') ws.cell(row=1, column=3, value='执导电影列表') row_num = 2 for director, data in movie_dict.items(): movie_list = ','.join(data['movies']) movie_count = len(data['movies']) ws.cell(row=row_num, column=1, value=director) ws.cell(row=row_num, column=2, value=movie_count) ws.cell(row=row_num, column=3, value=movie_list) row_num += 1 wb.save('D:\\pythonProject1\\电影信息统计.xlsx') director_list = [] movie_count_list = [] for director, data in sorted(movie_dict.items(), key=lambda x: len(x[1]['movies']), reverse=True): director_list.append(director) movie_count_list.append(len(data['movies'])) plt.rcParams['font.family'] = 'sans-serif' plt.rcParams['font.sans-serif'] = ['SimHei'] fig, ax = plt.subplots() ax.barh(director_list, movie_count_list) for i, director in enumerate(director_list): max_actor = [] for actor in movie_dict[director]['actors'].keys(): if movie_dict[director]['actors'][actor]==max(movie_dict[director]['actors'].values()): max_actor.append(actor) max_actor = str(max_actor) max_actor = max_actor.rstrip(']') max_actor = max_actor.lstrip('[') ax.annotate(max_actor, xy=(movie_count_list[i], i), xytext=(movie_count_list[i]+1, i), ha='left', va='center') ax.set_xlabel('执导电影数量') ax.set_ylabel('导演姓名') ax.invert_yaxis() plt.show()请帮我解释一下上述代码,详细一点
这段代码主要是对一个电影信息的文本文件进行处理,统计每个导演执导的电影数量和电影列表,并将结果保存到一个Excel文件中。同时,通过Matplotlib库绘制条形图,展示每个导演执导的电影数量,并在图表上标注每个导演最常用的演员。
具体来说,代码的实现如下:
1. 导入所需的库:
```
import openpyxl
import matplotlib.pyplot as plt
```
其中,openpyxl库用于处理Excel文件,matplotlib.pyplot库用于绘制图表。
2. 定义一个空字典movie_dict,用于存储电影信息:
```
movie_dict = {}
```
3. 打开电影信息的文本文件,逐行读取每条电影信息,将电影名称、导演和演员等信息存储到movie_dict中:
```
with open('D:\\pythonProject1\\电影信息.txt', 'r',encoding='utf-8') as f:
for line in f.readlines():
line = line.strip()
movie_info = line.split(';')
movie_name = movie_info[0]
directors = movie_info[1].split(',')
actors = movie_info[2].split(',')
for director in directors:
if director not in movie_dict:
movie_dict[director] = {'movies': [movie_name], 'actors': {}}
else:
movie_dict[director]['movies'].append(movie_name)
for actor in actors:
for director in directors:
if actor not in movie_dict[director]['actors']:
movie_dict[director]['actors'][actor] = 1
else:
movie_dict[director]['actors'][actor] += 1
```
这段代码使用了文件读写和字符串处理等基本操作。其中,strip()方法用于去除每行字符串两端的空格和换行符;split(';')方法用于将每行字符串按分号分割成电影名称、导演和演员等信息;split(',')方法用于将导演和演员等信息按逗号分割成列表。接着,使用for循环遍历每个导演和演员,将导演执导的电影名称存储到movie_dict中,同时统计每个导演最常用的演员。
4. 打开Excel文件,创建一个新的工作表,将导演执导的电影数量和电影列表写入工作表中:
```
wb = openpyxl.load_workbook('D:\\pythonProject1\\电影信息统计.xlsx')
ws = wb.create_sheet('导演作品统计',0)
ws.title = '导演作品统计'
ws.cell(row=1, column=1, value='导演姓名')
ws.cell(row=1, column=2, value='执导电影数量')
ws.cell(row=1, column=3, value='执导电影列表')
row_num = 2
for director, data in movie_dict.items():
movie_list = ','.join(data['movies'])
movie_count = len(data['movies'])
ws.cell(row=row_num, column=1, value=director)
ws.cell(row=row_num, column=2, value=movie_count)
ws.cell(row=row_num, column=3, value=movie_list)
row_num += 1
wb.save('D:\\pythonProject1\\电影信息统计.xlsx')
```
这段代码使用了openpyxl库中的相关方法,打开了一个已存在的Excel文件,创建了一个新的工作表,并将导演执导的电影数量和电影列表写入工作表中。具体来说,使用create_sheet()方法创建了一个名为“导演作品统计”的工作表,并设置了工作表的标题。然后,使用循环遍历movie_dict中的每个导演,将导演姓名、执导电影数量和电影列表写入工作表中。
5. 统计每个导演执导的电影数量,并使用Matplotlib库绘制条形图:
```
director_list = []
movie_count_list = []
for director, data in sorted(movie_dict.items(), key=lambda x: len(x[1]['movies']), reverse=True):
director_list.append(director)
movie_count_list.append(len(data['movies']))
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['SimHei']
fig, ax = plt.subplots()
ax.barh(director_list, movie_count_list)
for i, director in enumerate(director_list):
max_actor = []
for actor in movie_dict[director]['actors'].keys():
if movie_dict[director]['actors'][actor]==max(movie_dict[director]['actors'].values()):
max_actor.append(actor)
max_actor = str(max_actor)
max_actor = max_actor.rstrip(']')
max_actor = max_actor.lstrip('[')
ax.annotate(max_actor, xy=(movie_count_list[i], i), xytext=(movie_count_list[i]+1, i), ha='left', va='center')
ax.set_xlabel('执导电影数量')
ax.set_ylabel('导演姓名')
ax.invert_yaxis()
plt.show()
```
这段代码使用了Matplotlib库中的相关方法,统计了每个导演执导的电影数量,并使用barh()方法绘制了一个水平条形图。同时,使用annotate()方法在图表上标注了每个导演最常用的演员。最后,设置了x轴和y轴的标签,以及y轴反转等属性,并使用show()方法显示图表。
总体来说,这段代码主要是对电影信息进行处理和分析,并使用Python中的相关库将结果输出到Excel文件和图表中,展示了Python在数据处理和可视化方面的应用。
阅读全文