worksheett.cell(row=ii, column=9).value = str(result) 需要判断result如果=[],进不写入excel
时间: 2023-10-26 21:06:13 浏览: 32
如果要判断`result`是否为空列表`[]`,可以使用条件语句来实现。可以使用`if`语句来判断,如果`result`是空列表,则不写入Excel。下面是示例代码:
```python
if result != []:
worksheett.cell(row=ii, column=9).value = str(result)
```
这段代码中,我们使用`!=`运算符来判断`result`是否不等于空列表`[]`,如果不等于,则将`result`转换为字符串并写入Excel。如果等于空列表,则不执行写入操作。
相关问题
规范代码: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语句保持一致,每个语句需要换行,并且增加一个级别的缩进
- 对于较长的注释,可以在行尾使用`#`进行注释
规范代码:读取模板表格 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 = AutoTokenizer(value).parse() tokens = [(str(token.value) if token.type == Token.NUMBER else repr(token.value)) for token in tokens] # 运算 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)
规范代码如下:
```
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 = AutoTokenizer(value).parse()
tokens = [(str(token.value) if token.type == Token.NUMBER else repr(token.value)) for token in tokens]
# 运算
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)
```
修改内容:
1. 使用四个空格代替两个空格缩进。
2. 为每个代码块添加适当的空行,增强可读性。
3. 将注释与代码之间留出一个空格。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)