Skip to content

Instantly share code, notes, and snippets.

@sn-donbenjamin
Forked from magnetikonline/README.md
Created August 5, 2020 14:37
Show Gist options
  • Save sn-donbenjamin/c6aa902eabf128754cadb6b0462c540b to your computer and use it in GitHub Desktop.
Save sn-donbenjamin/c6aa902eabf128754cadb6b0462c540b to your computer and use it in GitHub Desktop.

Revisions

  1. @magnetikonline magnetikonline revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Python 2 json.dump() file-like object class for tab indenting of output file
    Using [`json.dump()`](https://docs.python.org/2/library/json.html#json.dump) with Python 2, `indent=` argument is limited to number of spaces only - no ability for tabs use.
    Using [`json.dump()`](https://docs.python.org/2/library/json.html#json.dump) with Python 2, `indent` argument is limited to number of spaces only - no ability for tabs use.

    Class `JSONTabIndentFileWriter` provides a [file-like object](https://docs.python.org/2/glossary.html#term-file-like-object) which will rewrite `json.dump()` output indending from spaces to tabs.
  2. @magnetikonline magnetikonline created this gist May 18, 2016.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # Python 2 json.dump() file-like object class for tab indenting of output file
    Using [`json.dump()`](https://docs.python.org/2/library/json.html#json.dump) with Python 2, `indent=` argument is limited to number of spaces only - no ability for tabs use.

    Class `JSONTabIndentFileWriter` provides a [file-like object](https://docs.python.org/2/glossary.html#term-file-like-object) which will rewrite `json.dump()` output indending from spaces to tabs.
    65 changes: 65 additions & 0 deletions jsontabindentfilewriter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    import json
    import re

    TEST_DICTIONARY = {
    'key1': 'value1',
    'key2': ['one','two','three','four'],
    'key3': {
    'apple': 'orange',
    'grape': 'banana',
    'tomato': ['five','six','seven','eight']
    }
    }

    JSON_SPACE_INDENT_COUNT = 4


    class JSONTabIndentFileWriter:
    def __init__(self,filepath,indent):
    # open JSON file
    self.fh = open(filepath,'w')

    # create indent find regular expression
    self.indent_regexp = re.compile(r'^([\[,]*\r?\n)( {{{0},}})(.*)'.format(indent))
    self.indent_size = indent

    def write(self,output):
    # match JSON builder indent output
    indent_match = self.indent_regexp.search(output)

    if (indent_match):
    # swap out space indents with tabs
    output = ''.join([
    indent_match.group(1), # optional "[" or "," character before indent
    '\t' * int(len(indent_match.group(2)) / self.indent_size), # indent conversion
    indent_match.group(3) # characters past indent
    ])

    # write output to file
    self.fh.write(output)

    def close(self):
    self.fh.close()

    def main():
    # create JSON tab indent file writer
    json_tab_file_write = JSONTabIndentFileWriter(
    'outfile.json',
    JSON_SPACE_INDENT_COUNT
    )

    # dump TEST_DICTIONARY to JSON with tab indenting
    json.dump(
    TEST_DICTIONARY,
    json_tab_file_write,
    indent = JSON_SPACE_INDENT_COUNT,
    separators = (',',': '),
    sort_keys = True
    )

    # close file writer
    json_tab_file_write.close()


    if (__name__ == '__main__'):
    main()