不对,打印信息放在targetTables循环的下面
时间: 2024-02-15 20:01:47 浏览: 21
好的,那就把打印信息的代码放到 `targetTables` 循环的外面。修改后的代码如下:
```
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']}")
print("Processed 100% 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']}")
if (i+1) % 100 == 0 or i+1 == table_count:
percent_complete = (i+1) / table_count * 100
print(f"Processed {percent_complete:.2f}% of tables")
```
这样在每处理完100个表或者处理完所有表之后,就会打印出当前处理的表所占的百分比。
table_dict = [] for table in 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']}") 请修改为打印targetTables数量和目前循环到第几个
可以在循环前添加一个计数器变量,并在循环内部打印出来。修改后的代码如下:
```
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']}")
print(f"Processed {i+1}/{table_count} tables")
```
这样在循环期间就会打印出当前处理的表的数量和总表数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)