2017-10-10 83 views
-1

歸類我有一個文件擴展名的詞典,其計這樣的:創建嵌套辭典蟒蛇

inp={'ldb': 25, 'baseB': 4, 'code': 100, 'cache-6': 55, 'Xauthority': 1, 'baseA': 4, 'fingerprint': 5, 'apmrc': 1, 'gz': 176, 'dbf': 1, 'lst': 2, 'markdown': 10, 'sqlite-shm': 4, 'config': 1, 'ps1': 8, 'vsixmanifest': 4, 'pkl': 5, 'gitignore': 8, 'xml': 44, '22': 1, 'la': 9, 'sublime-keymap': 1, 'cache': 103, 'jar': 1, 'ts': 704, 'desktop': 3, 'source': 1, 'sqlite3-journal': 1, 'TAG': 1, '4': 1, 'usage': 6, 'oTN4k5': 1, 'bin': 7, 'docx': 9, 'rb': 1, 'woff': 3, 'db': 17, 'V2XO5Y': 1, 'dat': 3, 'npmrc': 1, 'lz4': 1, 'name': 2, 'exe': 21, 'bat': 5, 'bau': 1, 'file extension': {}, 'converted-launchers': 1, 'crt': 8, 'h': 122, '56': 3, 'list': 2, 'dll': 42, 'patch': 2, 'zsh-update': 1, 'editorconfig': 4, 'stderr': 8, 'sublime_session': 1, 'xls': 1, 'bak': 1, 'bond': 108, 'node': 16, 'cmd': 2, 'old': 26, 'babelrc': 2, 'locale': 1, 'cfg': 3, 'htm': 1, 'odt': 1, 'keyring': 1, 'xlb': 2, 'gypi': 1, 'md~': 1, 'sqlite-journal': 1, 'odb': 2, 'dic': 1, 'todo': 1, 'zcompdump': 1, 'tvc': 1, 'out': 20, 'tmp': 4, 'stats': 2, 'icc': 1, 'zprofile': 1, 'dbt': 1, 're': 21, '3': 1, '7': 9, 'ics': 3, 'sys': 1, 'reg': 3, 'php': 12, 'metadata': 6, 'dirs': 1, 'whl': 2, 'run': 1, 'asar': 1, 'scpt': 2, 'tdb': 3, 'journal': 2, 'zshrc': 1, 'little': 2, 'pub': 1, 'js': 4336, 'asc': 1, 'base': 1, 'key': 12, 'xbel': 1, 'properties': 2, 'bash': 1, 'eslintignore': 1, 'c': 283, 'zip': 6, 'idx': 5, 'lsup7I': 1, 'jshintrc': 6, 'un~': 2, 'vbs': 1, 'rdf': 2, 'dmrc': 1, 'Mdg80A': 1, 'sbstore': 42, 'pdf': 18, 'tmLanguage': 11, 'xlc': 2, 'jsonlz4': 39, 'tgz': 2, 'pset': 42, 'iml': 2, 'xz': 1, 'bashrc': 1, 'db-journal': 3, 'pf2': 5, 'localstorage': 94, 'yaml': 8, 'tmPreferences': 4, 'isrunning': 1, 'txt': 287, 'orig': 2, 'gvdb': 1, 'xpi': 9, 'git': 1, 'gitmodules': 1, 'log': 47, 'swo': 1, '0': 11, 'stamp': 2, 'vxd': 1, 'fmt': 1, 'py': 480, 'sock': 2, 'pb': 32, 'gif': 3, 'json': 2130, '2': 2, 'js-20170612122310': 1, '6': 1, 'swp': 2, 'bash_logout': 1, 'final': 1, 'pl': 1, 'gpg-agent': 1, 'a': 2, 'sdv': 1, 'x86_64-pc-linux-gnu': 1, 'parentlock': 2, 'cson': 8, 'rcache': 2, 'otf': 29, 'sublime-settings': 1, 'xinputrc': 1, 'gitconfig': 1, 'npmignore': 84, 'localstorage-journal': 94, 'gyp': 1, 'rst': 11, '1vCfy0': 1, 'bnf': 1, 'png': 2314, 'db-shm': 1, 'info': 1, 'md': 844, 'js-20170816210634': 1, 'sublime-snippet': 14, 'tsv': 1, 'gpg': 1, 'sth': 38, 'mk': 5, 'watchr': 1, 'prl': 9, 'sqlite-wal': 4, 'pem': 4, 'deb': 3, 'zsh': 235, 'pack': 5, 'zsh_history': 1, 'sqlite': 24, 'tar': 1, 'stdout': 8, 'jscsrc': 1, 'lock': 3, 'pro': 1, 'gitkeep': 2, 'coffee': 8, 'jpg': 23, 'jamignore': 1, 'sample': 72, 'ino': 1, 'jshintignore': 1, 'ini': 14, 'conf': 17, 'xcu': 1, 'gradle': 3, 'plist': 1, 'xsession-errors': 1, 'keystore': 1, 'nls': 1, 'sudo_as_admin_successful': 1, 'spec': 9, 'yml': 47, 'thm': 1, '1': 8, 'html': 89, '5': 10, 'MZPZ5Y': 1, 'sqlite3': 1, 'pak': 61, 'ttf': 109, 'css': 112, 'profile': 1, 'map': 84, 'update-timestamp': 1, 'less': 1, 'metadata-v2': 6, 'zsh-template': 1, 'zsh-theme': 143, 'trashinfo': 3, 'ICEauthority': 1, 'sublime-build': 1, 'pym': 16, 'ico': 1, 'pyc': 51, 'B5S94Y': 1, 'cc': 81, 'viminfo': 1, 'scss': 2, 'bash_history': 1, 'exc': 1, 'java': 7, 'svg': 187, 'eslintrc': 6, 'm': 1, 'DB': 4, 'JPG': 1, 'mozlz4': 2, 'sh': 41, 'so': 18, 'eot': 1, 'kbx': 1, 'xba': 1, 'bdic': 1, 'opts': 11, 'db-wal': 1} 

我要歸類在嵌套的字典文件擴展名,所以我想這樣的輸出:

{'file_ext_count':{'ldb': 25, 'baseB': 4, 'code': 100, 'cache-6': 55, 'Xauthority': 1, 'baseA': 4, 'fingerprint': 5, 'apmrc': 1, 'gz': 176, 'dbf': 1, 'lst': 2, 'markdown': 10, 'sqlite-shm': 4, 'config': 1, 'ps1': 8, 'vsixmanifest': 4, 'pkl': 5, 'gitignore': 8, 'xml': 44, '22': 1, 'la': 9, 'sublime-keymap': 1, 'cache': 103, 'jar': 1, 'ts': 704, 'desktop': 3, 'source': 1, 'sqlite3-journal': 1, 'TAG': 1, '4': 1, 'usage': 6, 'oTN4k5': 1, 'bin': 7, 'docx': 9, 'rb': 1, 'woff': 3, 'db': 17, 'V2XO5Y': 1, 'dat': 3, 'npmrc': 1, 'lz4': 1, 'name': 2, 'exe': 21, 'bat': 5, 'bau': 1, 'file extension': {}, 'converted-launchers': 1, 'crt': 8, 'h': 122, '56': 3, 'list': 2, 'dll': 42, 'patch': 2, 'zsh-update': 1, 'editorconfig': 4, 'stderr': 8, 'sublime_session': 1, 'xls': 1, 'bak': 1, 'bond': 108, 'node': 16, 'cmd': 2, 'old': 26, 'babelrc': 2, 'locale': 1, 'cfg': 3, 'htm': 1, 'odt': 1, 'keyring': 1, 'xlb': 2, 'gypi': 1, 'md~': 1, 'sqlite-journal': 1, 'odb': 2, 'dic': 1, 'todo': 1, 'zcompdump': 1, 'tvc': 1, 'out': 20, 'tmp': 4, 'stats': 2, 'icc': 1, 'zprofile': 1, 'dbt': 1, 're': 21, '3': 1, '7': 9, 'ics': 3, 'sys': 1, 'reg': 3, 'php': 12, 'metadata': 6, 'dirs': 1, 'whl': 2, 'run': 1, 'asar': 1, 'scpt': 2, 'tdb': 3, 'journal': 2, 'zshrc': 1, 'little': 2, 'pub': 1, 'js': 4336, 'asc': 1, 'base': 1, 'key': 12, 'xbel': 1, 'properties': 2, 'bash': 1, 'eslintignore': 1, 'c': 283, 'zip': 6, 'idx': 5, 'lsup7I': 1, 'jshintrc': 6, 'un~': 2, 'vbs': 1, 'rdf': 2, 'dmrc': 1, 'Mdg80A': 1, 'sbstore': 42, 'pdf': 18, 'tmLanguage': 11, 'xlc': 2, 'jsonlz4': 39, 'tgz': 2, 'pset': 42, 'iml': 2, 'xz': 1, 'bashrc': 1, 'db-journal': 3, 'pf2': 5, 'localstorage': 94, 'yaml': 8, 'tmPreferences': 4, 'isrunning': 1, 'txt': 287, 'orig': 2, 'gvdb': 1, 'xpi': 9, 'git': 1, 'gitmodules': 1, 'log': 47, 'swo': 1, '0': 11, 'stamp': 2, 'vxd': 1, 'fmt': 1, 'py': 480, 'sock': 2, 'pb': 32, 'gif': 3, 'json': 2130, '2': 2, 'js-20170612122310': 1, '6': 1, 'swp': 2, 'bash_logout': 1, 'final': 1, 'pl': 1, 'gpg-agent': 1, 'a': 2, 'sdv': 1, 'x86_64-pc-linux-gnu': 1, 'parentlock': 2, 'cson': 8, 'rcache': 2, 'otf': 29, 'sublime-settings': 1, 'xinputrc': 1, 'gitconfig': 1, 'npmignore': 84, 'localstorage-journal': 94, 'gyp': 1, 'rst': 11, '1vCfy0': 1, 'bnf': 1, 'png': 2314, 'db-shm': 1, 'info': 1, 'md': 844, 'js-20170816210634': 1, 'sublime-snippet': 14, 'tsv': 1, 'gpg': 1, 'sth': 38, 'mk': 5, 'watchr': 1, 'prl': 9, 'sqlite-wal': 4, 'pem': 4, 'deb': 3, 'zsh': 235, 'pack': 5, 'zsh_history': 1, 'sqlite': 24, 'tar': 1, 'stdout': 8, 'jscsrc': 1, 'lock': 3, 'pro': 1, 'gitkeep': 2, 'coffee': 8, 'jpg': 23, 'jamignore': 1, 'sample': 72, 'ino': 1, 'jshintignore': 1, 'ini': 14, 'conf': 17, 'xcu': 1, 'gradle': 3, 'plist': 1, 'xsession-errors': 1, 'keystore': 1, 'nls': 1, 'sudo_as_admin_successful': 1, 'spec': 9, 'yml': 47, 'thm': 1, '1': 8, 'html': 89, '5': 10, 'MZPZ5Y': 1, 'sqlite3': 1, 'pak': 61, 'ttf': 109, 'css': 112, 'profile': 1, 'map': 84, 'update-timestamp': 1, 'less': 1, 'metadata-v2': 6, 'zsh-template': 1, 'zsh-theme': 143, 'trashinfo': 3, 'ICEauthority': 1, 'sublime-build': 1, 'pym': 16, 'ico': 1, 'pyc': 51, 'B5S94Y': 1, 'cc': 81, 'viminfo': 1, 'scss': 2, 'bash_history': 1, 'exc': 1, 'java': 7, 'svg': 187, 'eslintrc': 6, 'm': 1, 'DB': 4, 'JPG': 1, 'mozlz4': 2, 'sh': 41, 'so': 18, 'eot': 1, 'kbx': 1, 'xba': 1, 'bdic': 1, 'opts': 11, 'db-wal': 1}} 

後來我想解析這個到json 幫助這個python初學者 謝謝!!

回答

2

只需使用json.dumps()創建JSON字符串。

import json 
json_string = json.dumps({'file_ext_count':inp}, indent=3) 

然後你可以寫入文件或做任何你想要的字符串。

1

嵌套字典可以這樣創造:

new_dct = {} 
new_dct['file_ext_count'] = inp 
+0

此外,帶有json.dump方法的json庫(import json)可以將您的對象轉換爲json格式的流。 – ShreyasG

+0

它是一個合適的命名方式!或者我應該使用數組在字典中,哪種方法適用於json對象的創建? – Sjn73

+0

檢查Josh的回答,這是獲取json_object的最佳方法。 – ShreyasG