改为打印百分比
时间: 2023-06-21 07:21:21 浏览: 40
可以根据表的数量计算出每个表所占的百分比,然后在循环内部打印出来。修改后的代码如下:
```
table_dict = []
table_count = len(targetTables)
for i, table in enumerate(targetTables):
grants = spark.sql(f'show GRANT on {table}').collect()
for grant in grants:
table_dict.append(f"{json_dict['orgId']}|{''}|{table}|{grant['Principal']}|{grant['ActionType']}|{grant['ObjectType']}|{grant['ObjectKey']}")
percent_complete = (i+1) / table_count * 100
print(f"Processed {percent_complete:.2f}% of tables")
```
这样在循环期间就会打印出当前处理的表所占的百分比。注意,这里使用了 `:.2f` 格式化字符串来保留小数点后两位。
相关问题
打印改为每100次显示一次百分比
可以通过控制循环内部的计数器变量来实现每100次打印一次百分比。修改后的代码如下:
```
table_dict = []
table_count = len(targetTables)
for i, table in enumerate(targetTables):
grants = spark.sql(f'show GRANT on {table}').collect()
for grant in grants:
table_dict.append(f"{json_dict['orgId']}|{''}|{table}|{grant['Principal']}|{grant['ActionType']}|{grant['ObjectType']}|{grant['ObjectKey']}")
if (i+1) % 100 == 0:
percent_complete = (i+1) / table_count * 100
print(f"Processed {percent_complete:.2f}% of tables")
```
这样在每处理完100个表之后,就会打印出当前处理的表所占的百分比。
matlab 百分数
要在 MATLAB 中表示百分数,你可以使用 `fprintf` 函数或 `sprintf` 函数以及百分号符号 `%`。
下面是一个示例代码,将一个数值表示为百分数并打印输出:
```matlab
num = 0.75;
percentage = num * 100;
fprintf('The percentage is: %.2f%%\n', percentage);
```
这个代码会输出:`The percentage is: 75.00%`
在这个示例中,我们将 `num` 值乘以 100 来得到百分数,然后使用 `fprintf` 函数来打印输出。格式字符串 `%.2f` 表示保留两位小数,而 `%` 符号后面的 `%%` 表示输出一个百分号。
你可以根据需要修改代码中的数值和格式字符串来适应不同的情况。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)