_AD_.Result = "{:.2f}".format(_AD_.Original) Traceback (most recent last): 根据你说的,报了这个错误
时间: 2024-10-23 20:14:02 浏览: 36
如果运行这段代码后遇到`FormatError`或者类似`ValueError`的`Traceback`,通常意味着你在尝试格式化的时候遇到了问题。可能是以下几个原因:
1. **数据类型问题**:`{:.2f}`期望的是一个浮点数,如果 `_AD_.Original` 的类型不是浮点数,如整数、复数或其他非数值类型,就会触发异常。
2. **空值处理**:如果你试图格式化的是None或者其他类型的空值,也会出错。需要先检查`_AD_.Original`是否为空再进行格式化。
3. **数值溢出**:如果原始数字太大,超过了Python能表示的最大精度,格式化也会失败。
解决方法通常是检查并确保`_AD_.Original`的数据类型和内容都是正确的,可以添加一些条件判断来处理可能的问题,例如:
```python
if isinstance(_AD_.Original, (int, float)):
_AD_.Result = "{:.2f}".format(_AD_.Original)
else:
_AD_.Result = "无法格式化" # 或者抛出异常
```
或者使用Python的异常处理机制,比如`try-except`块。
相关问题
用xlwings支持的方式规范改写这段代码:import xlwings as xw import os def main(): mould_excel_path = r'退汇模板.xlsx' ori_excel_path = r'数据源.xlsx' result_folder = './退汇表' if not os.path.exists(result_folder): os.makedirs(result_folder) app = xw.App(visible=False, add_book=False) ori_wb = app.books.open(ori_excel_path) ori_sheet = ori_wb.sheets[0] ori_sheet.calculate() # 计算公式 rows = ori_sheet.used_range.last_cell.row cols = ori_sheet.used_range.last_cell.column for r in range(2, rows + 1): info = {} mould_wb = xw.Book(mould_excel_path) mould_sheet = mould_wb.sheets[0] for c in range(1, cols + 1): if ori_sheet[c, 1].value: info[ori_sheet[c, 1].value] = ori_sheet[r, c].value print(info) mould_sheet.range('F1').value = info['日期'] mould_sheet.range('J1').value = info['凭证号'] mould_sheet.range('J3').value = info['主体\\姓名'] mould_sheet.range('J4').value = info['账户'] mould_sheet.range('J5').value = info['开户行'] mould_sheet.range('J7').value = info['支付金额'] mould_sheet.range('G9').value = info['预算文号'] mould_sheet.range('J10').value = info['款项用途'] result_file_path = os.path.join(result_folder, f"{info['文件名']}.xlsx") mould_wb.save(result_file_path) mould_wb.close() ori_wb.close() app.quit() if __name__ == '__main__': try: main() except Exception as e: input(f"error line:{e.__traceback__.tb_lineno}-{e}")
import xlwings as xw
import os
def main():
mould_excel_path = r'退汇模板.xlsx'
ori_excel_path = r'数据源.xlsx'
result_folder = './退汇表'
if not os.path.exists(result_folder):
os.makedirs(result_folder)
app = xw.App(visible=False, add_book=False)
ori_wb = app.books.open(ori_excel_path)
ori_sheet = ori_wb.sheets[0]
ori_sheet.calculate() # 计算公式
rows, cols = ori_sheet.used_range.last_cell.row, ori_sheet.used_range.last_cell.column
for r in range(2, rows + 1):
info = {}
mould_wb = xw.Book(mould_excel_path)
mould_sheet = mould_wb.sheets[0]
for c in range(1, cols + 1):
if ori_sheet[c, 1].value:
info[ori_sheet[c, 1].value] = ori_sheet[r, c].value
print(info)
mould_sheet.range('F1').value = info.get('日期', None)
mould_sheet.range('J1').value = info.get('凭证号', None)
mould_sheet.range('J3').value = info.get('主体\\姓名', None)
mould_sheet.range('J4').value = info.get('账户', None)
mould_sheet.range('J5').value = info.get('开户行', None)
mould_sheet.range('J7').value = info.get('支付金额', None)
mould_sheet.range('G9').value = info.get('预算文号', None)
mould_sheet.range('J10').value = info.get('款项用途', None)
result_file_path = os.path.join(result_folder, f"{info.get('文件名', None)}.xlsx")
mould_wb.save(result_file_path)
mould_wb.close()
ori_wb.close()
app.quit()
if __name__ == '__main__':
try:
main()
except Exception as e:
input(f"error line:{e.__traceback__.tb_lineno}-{e}")
Traceback (most recent call last): File "dense_pcd.py", line 7, in <module> create_dense_point_cloud(model_root, save_root) File "/home/chao/anaconda3/envs/suctionnet/lib/python3.7/site-packages/suctionnetAPI/create_dense_pcd.py", line 33, in create_dense_point_cloud v_poisson, n_poisson = pcu.sample_mesh_poisson_disk(v, f, n, num_samples=-1, radius=0.0002, use_geodesic_distance=True) TypeError: sample_mesh_poisson_disk(): incompatible function arguments. The following argument types are supported: 1. (v: numpy.ndarray, f: numpy.ndarray, num_samples: int, radius: float = 0.0, use_geodesic_distance: bool = True, best_choice_sampling: bool = True, random_seed: int = 0, sample_num_tolerance: float = 0.04, oversampling_factor: float = 40.0) -> Tuple[object, object]
这是一个 Python 错误信息,看起来是 create_dense_pcd.py 中的 sample_mesh_poisson_disk 函数调用出错了。错误信息中提到该函数支持的参数类型,但是传入的参数类型与支持的不一致。需要检查传入该函数的参数类型是否正确,或者尝试更改函数的参数类型以适应当前的参数。
阅读全文