规范代码:info[ori_sheet.cell(1, c).value]=ori_sheet.cell(r, c).value
时间: 2024-01-22 14:21:29 浏览: 95
可以将这行代码规范化为:
```
info[ori_sheet.cell(row=1, column=c).value] = ori_sheet.cell(row=r, column=c).value
```
通过使用 `row` 和 `column` 参数来明确指定单元格的行和列,可以增加代码的可读性。同时,建议在参数中显式地指定行和列,而不是使用位置参数,以避免可能的混淆。
相关问题
规范代码:for c in range(1, cols + 1): if ori_sheet.cell(1, c).value: if ori_sheet.cell(r, c).data_type == 'f': # 如果单元格内是函数公式,则运算后写入 formula = ori_sheet.cell(r, c).value tokens = Tokenizer(formula).parse() for token in tokens: if token.type == Token.NUMBER: token.value = str(token.value) elif token.type == Token.STRING: token.value = repr(token.value) result = tokens.to_tree().calculate() mould_sheet.cell(row=r, column=c, value=result) else: # 如果单元格内不是函数公式,则直接写入 info[ori_sheet.cell(1, c).value] = ori_sheet.cell(r, c).value # 把每一行需要的信息加入字典 mould_sheet.cell(row=r, column=c, value=ori_sheet.cell(r, c).value)
以下是规范化后的代码:
```
for c in range(1, cols + 1):
if ori_sheet.cell(1, c).value:
if ori_sheet.cell(r, c).data_type == 'f': # 如果单元格内是函数公式,则运算后写入
formula = ori_sheet.cell(r, c).value
tokens = Tokenizer(formula).parse()
for token in tokens:
if token.type == Token.NUMBER:
token.value = str(token.value)
elif token.type == Token.STRING:
token.value = repr(token.value)
result = tokens.to_tree().calculate()
mould_sheet.cell(row=r, column=c, value=result)
else: # 如果单元格内不是函数公式,则直接写入
info[ori_sheet.cell(1, c).value] = ori_sheet.cell(r, c).value # 把每一行需要的信息加入字典
mould_sheet.cell(row=r, column=c, value=ori_sheet.cell(r, c).value)
```
修改内容:
- 代码块需要缩进,使用四个空格进行缩进
- 在if语句中,每个条件需要换行,并且缩进增加一个级别
- 在for循环中,每个语句需要换行,并且增加一个级别的缩进
- 在else语句中,与前面的if语句保持一致,每个语句需要换行,并且增加一个级别的缩进
- 对于较长的注释,可以在行尾使用`#`进行注释
规范代码:读取数据源表格 ori_excel = openpyxl.load_workbook(ori_excel_path) ori_sheet = ori_excel.active rows = ori_sheet.max_row cols = ori_sheet.max_column 加载公式解析器 from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("microsoft/CodeGPT-small-py") 遍历每一行 for r in range(2, rows + 1): info = {} # 定义一个字典用来存放数据 mould_excel = openpyxl.load_workbook(mould_excel_path) mould_sheet = mould_excel.active # 遍历每一列 for c in range(1, cols + 1): # 如果标题栏和单元格内有内容 ori_cell_value = ori_sheet.cell(1, c).value cell_value = ori_sheet.cell(r, c).value if ori_cell_value and cell_value: # 获取单元格数据类型和值 data_type = ori_sheet.cell(r, c).data_type value = cell_value # 如果单元格内是函数公式,则运算后写入 if data_type == 'f': # 公式解析 tokens = [str(token.value) if token.type == Token.NUMBER else repr(token.value) for token in tokenizer.parse(value)] # 运算 result = calculate(tokens) # 写入结果 mould_sheet.cell(row=r, column=c).value = result else: # 直接写入值 mould_sheet.cell(row=r, column=c).value = value # 将计算结果写入到模板表格 if data_type == 'f': mould_sheet.cell(row=r, column=c, value=result) else: mould_sheet.cell(row=r, column=c, value=value) else: # 如果单元格内不是函数公式,则直接写入 info[ori_cell_value] = cell_value mould_sheet.cell(row=r, column=c, value=cell_value)
下面是规范化后的代码:
```python
import openpyxl
from transformers import AutoTokenizer
# 读取数据源表格
ori_excel = openpyxl.load_workbook(ori_excel_path)
ori_sheet = ori_excel.active
rows = ori_sheet.max_row
cols = ori_sheet.max_column
# 加载公式解析器
tokenizer = AutoTokenizer.from_pretrained("microsoft/CodeGPT-small-py")
# 遍历每一行
for r in range(2, rows + 1):
info = {}
# 定义一个字典用来存放数据
mould_excel = openpyxl.load_workbook(mould_excel_path)
mould_sheet = mould_excel.active
# 遍历每一列
for c in range(1, cols + 1):
# 如果标题栏和单元格内有内容
ori_cell_value = ori_sheet.cell(1, c).value
cell_value = ori_sheet.cell(r, c).value
if ori_cell_value and cell_value:
# 获取单元格数据类型和值
data_type = ori_sheet.cell(r, c).data_type
value = cell_value
# 如果单元格内是函数公式,则运算后写入
if data_type == 'f':
# 公式解析
tokens = [str(token.value) if token.type == Token.NUMBER else repr(token.value) for token in tokenizer.parse(value)]
# 运算
result = calculate(tokens)
# 写入结果
mould_sheet.cell(row=r, column=c).value = result
else:
# 直接写入值
mould_sheet.cell(row=r, column=c).value = value
# 将计算结果写入到模板表格
if data_type == 'f':
mould_sheet.cell(row=r, column=c, value=result)
else:
mould_sheet.cell(row=r, column=c, value=value)
else:
# 如果单元格内不是函数公式,则直接写入
info[ori_cell_value] = cell_value
mould_sheet.cell(row=r, column=c, value=cell_value)
```
阅读全文
相关推荐

















