确定列表中元素的类型

Determine type of element from list

我的 python 代码正在使用 PEM 包读取包含证书和私钥的 PEM 文件。

代码:

import pem
Mylist = pem.parse_file(r"C:\Desktop\MyPemFile.pem")
for ele in Mylist:
     print(type(ele))

输出:

<class 'pem._core.RSAPrivateKey'>
<class 'pem._core.Certificate'>
<class 'pem._core.Certificate'>
<class 'pem._core.Certificate'>
<class 'pem._core.Certificate'> 

现在,我正在尝试识别列表中每个元素的类型并执行某些操作。
但是,我无法确定类型。如何修改我的代码以确定每个元素的类型?

代码:

Mylist = pem.parse_file(r"C:\Desktop\MyPemFile.pem")
for ele in Mylist:
     if type(ele) == 'pem._core.RSAPrivateKey':
             print(ele) # Control doesn't go here. 
     else:
             print("Invalid type")

输出:

Invalid type
Invalid type
Invalid type
Invalid type
Invalid type 

应该有一种简单的方法可以从 pem 中导入类型本身(而不是字符串标识符)。可能类似于 from pem import RSAPrivateKey(我从未使用过该库)。那么你可以这样做:

if isinstance(ele, RSAPrivateKey):
    print(ele)