Python 将目录结构转换为json?
我想转换一个项目层次结构,其中包含JSON字典中每个类或包的一些属性。比如说 我正在为此使用python。谁能给我一些基本的提纲吗。我该如何实现这一点。谢谢。因此,我想要这样的东西:Python 将目录结构转换为json?,python,json,Python,Json,我想转换一个项目层次结构,其中包含JSON字典中每个类或包的一些属性。比如说 我正在为此使用python。谁能给我一些基本的提纲吗。我该如何实现这一点。谢谢。因此,我想要这样的东西: a = package, a.b = package, a.b.c = class, a.b.d = class j_string = { "my_data": { "a": "info": {}, "b": { "info": {},
a = package, a.b = package, a.b.c = class, a.b.d = class
j_string = {
"my_data": {
"a": "info": {},
"b": {
"info": {},
"c": {
"info": {}
},
"d": {
"info": {}
}
}
}
}
尝试使用
pkgutil.walk\u软件包
与importlib.import\u模块
结合使用
def get_all_module_of_a_package(pkg_name):
pkg = importlib.import_module(pkg_name)
for importer, name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__ + '.'):
if is_pkg:
continue
print name # I just print them, you can build a dict you desired
# do some other things (if you want to get member of a module)
# use inspect.getmembers
>>> get_all_module_of_a_package('json')
json.decoder
json.encoder
json.scanner
json.tests.test_check_circular
json.tests.test_decode
json.tests.test_default
json.tests.test_dump
json.tests.test_encode_basestring_ascii
json.tests.test_fail
json.tests.test_float
json.tests.test_indent
json.tests.test_pass1
json.tests.test_pass2
json.tests.test_pass3
json.tests.test_recursion
json.tests.test_scanstring
json.tests.test_separators
json.tests.test_speedups
json.tests.test_tool
json.tests.test_unicode
json.tool
尝试使用
pkgutil.walk\u软件包
与importlib.import\u模块
结合使用
def get_all_module_of_a_package(pkg_name):
pkg = importlib.import_module(pkg_name)
for importer, name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__ + '.'):
if is_pkg:
continue
print name # I just print them, you can build a dict you desired
# do some other things (if you want to get member of a module)
# use inspect.getmembers
>>> get_all_module_of_a_package('json')
json.decoder
json.encoder
json.scanner
json.tests.test_check_circular
json.tests.test_decode
json.tests.test_default
json.tests.test_dump
json.tests.test_encode_basestring_ascii
json.tests.test_fail
json.tests.test_float
json.tests.test_indent
json.tests.test_pass1
json.tests.test_pass2
json.tests.test_pass3
json.tests.test_recursion
json.tests.test_scanstring
json.tests.test_separators
json.tests.test_speedups
json.tests.test_tool
json.tests.test_unicode
json.tool
importjason;jason.loads(j_string)
-但是您的字符串设计为断开的,因为它是用“
封装的,并且您没有逃过内部”
slement。我建议在字符串周围使用”
。谁是jason?-<代码>导入json可能类似于实际数据的东西不是来自我的系统,所以我只是以字符串的形式获取路径。我必须使用“.”分隔符来解析它,而“项目层次结构”是指磁盘上目录和文件的目录集合吗?您如何发现属性。。。比如你怎么知道a是一个“包”,a.b.d是一个“类”?import;jason.loads(j_string)
-但是您的字符串设计为断开的,因为它是用“
封装的,并且您没有逃过内部”
slement。我建议在字符串周围使用”
。谁是jason?-<代码>导入json可能类似于实际数据的东西不是来自我的系统,所以我只是以字符串的形式获取路径。我必须使用“.”分隔符来解析它,而“项目层次结构”是指磁盘上目录和文件的目录集合吗?您如何发现属性。。。比如你怎么知道a是一个“包”,a.b.d是一个“类”?