打印改为每100次显示一次百分比
时间: 2023-08-22 16:09:31 浏览: 41
可以通过控制循环内部的计数器变量来实现每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个表之后,就会打印出当前处理的表所占的百分比。
相关问题
改为打印百分比
可以根据表的数量计算出每个表所占的百分比,然后在循环内部打印出来。修改后的代码如下:
```
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` 格式化字符串来保留小数点后两位。
用一条sql语句显示数据百分比并加百分号
以下是可以用的 SQL 语句:
SELECT CONCAT(ROUND((COUNT(*) * 100 / (SELECT COUNT(*) FROM your_table)), 2), '%') AS percentage FROM your_table;
这条 SQL 语句可以计算出你所选的表中的数据百分比,并将结果添加百分号。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)