python-处理Excel表数据
需求:
获取指定的表单2018,去除表单中月份带*的工资。最后计算所有月份的工资。

实现的代码块:
"""
@Project :python
@Author : 文跃锐(yuerwen)
@Time : 2021/11/08
@File :yuerwen_test_去掉包含星号的月份收入.py
"""
'''
那么我们怎么在汇总收入中,去掉包含星号的月份收入呢?
就需要我们查出哪些月份是带星号的,不要统计在内。
'''
import xlrd# 打开Excel表格 返回book对象
book = xlrd.open_workbook('income.xlsx')# 获取表单对象
sheet_obj = book.sheet_by_name('2018')
# 获取列的所有数据,返回list
colx_incomes = sheet_obj.col_values(colx=1, start_rowx=1)
print(f'所有工资的总和:{sum(colx_incomes)} 元')# 算法:去除带*的月份的工资
# 初始指定工资
start_incomes = 0
# 月份在第1列
monthes = sheet_obj.col_values(colx=0)
# 循环获取带*号的数据,存储到指定变量中
for row, month in enumerate(monthes):# 判断数据是否是字符串并且*结尾if type(month) is str and month.endswith('*'):# 获取指定表格中的数据incomes = sheet_obj.cell_value(row, 1)print(month, incomes)# 拿到的数据加到变量中start_incomes += incomesprint(f"2018年真实收入为: {int(sum(colx_incomes)- start_incomes)} 元")
2021-11-8 23:44 更新
内容:计算Excel中所有表中,去除带*的工资:
代码块:
"""
@Project :python
@Author : 文跃锐(yuerwen)
@Time : 2021/11/08
@File :yuerwen_test_3years.py
"""import xlrd
book = xlrd.open_workbook('income.xlsx')sheets = book.sheets()income_of_3years = 0for sheet in sheets:# 获取列的所有数据,返回listcolx_incomes = sheet.col_values(colx=1, start_rowx=1)print(f'{sheet.name}年所有工资的总和:{sum(colx_incomes)} 元')# 算法:去除带*的月份的工资# 初始指定工资start_incomes = 0# 月份在第1列monthes = sheet.col_values(colx=0)for row, month in enumerate(monthes):if type(month) is str and month.endswith('*'):incomes = sheet.cell_value(row, 1)print(month, incomes)start_incomes += incomesprint(f"{sheet.name}年去除带*后真实收入为: {int(sum(colx_incomes) - start_incomes)} 元")income_of_3years += int(sum(colx_incomes) - start_incomes)print('-----------------------------------------------------------------------')
print(f'全部收入为{income_of_3years}')
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
