Auto commit

上级 df352508
import openpyxl
# 读取指定单元格的值
def read_excel(file_path, sheet_name, row, col):
# 打开 Excel 文件
wb = openpyxl.load_workbook(file_path)
# 打开指定的 sheet
sheet = wb[sheet_name]
# 读取指定单元格的值
cell = sheet.cell(row=row, column=col)
value = cell.value
# 关闭文件
wb.close()
return value
# 写入指定单元格的值
def write_excel(file_path, sheet_name, row, col, value):
# 打开 Excel 文件
wb = openpyxl.load_workbook(file_path)
# 打开指定的 sheet
sheet = wb[sheet_name]
# 写入指定单元格的值
cell = sheet.cell(row=row, column=col)
cell.value = value
# 保存文件
wb.save(file_path)
# 关闭文件
wb.close()
# 测试代码
if __name__ == '__main__':
# 读取指定单元格的值
file_path = 'example.xlsx'
sheet_name = 'Sheet1'
row = 2
col = 3
value = read_excel(file_path, sheet_name, row, col)
print(f'第 {row} 行,第 {col} 列的值为:{value}')
# 写入指定单元格的值
row = 3
col = 4
value = 'Hello World'
write_excel(file_path, sheet_name, row, col, value)
print(f'已将 {value} 写入第 {row} 行,第 {col} 列')
print('欢迎来到 InsCode')
\ No newline at end of file
# 定义外部函数,接受一个参数x
def outer_func(x):
# 定义内部函数,接受一个参数y
def inner_func(y):
return x + y # 将加号加入计算
# 返回内部函数
return inner_func
# 调用外部函数,将返回的内部函数赋值给变量
closure_func = outer_func(5)
# 调用闭包函数,将结果赋值给变量
result = closure_func(10)
# 输出结果
print(result)
def test_closure_func():
# 调用外部函数
closure_func = outer_func(5)
# 调用闭包函数,将结果与期望值比较
assert closure_func(10) == 15
assert closure_func(20) == 25
assert closure_func(-5) == 0
test_closure_func()
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册