对于我正在处理的项目,我需要将字符串(其中包含ID号(即ID_00000001 ... ID_00002032或类似内容))转换为一类。 我的意思是,已经有存储类有像这样的值:在Python中将字符串转换为类(反之亦然)
class ID_00000001:
name = 'Robert Johnson'
age = 193
facebook_url = 'facebook.com/Robert_johnson'
etc.
我想让它,这样我可以此配置文件和别人比较。我想这样做的方式是比较喜欢的值(psudo):
current_profile_str = 'ID_00000001'
for i in range(len(all_IDs)):
matches = []
num = *converts number into 8 digit str*
cycle_profile_str = 'ID_' + num
*convert current_profile and cycle_profile to class*
if (current_profile.age == cycle_profile.age):
matches.append(cycle_profile_str)
所以,我想我在这方面的问题,是我会怎样能够将字符串转换为一类?
感谢先进!
你为什么不用字典? – ppperry
我有一种感觉,你正在使用有点奇怪的有点.... –
看起来你像混淆类与实例。在进一步学习之前更好地[阅读它](https://docs.python.org/2/tutorial/classes.html) – alfasin