Skip to content

Instantly share code, notes, and snippets.

@ffr4nz
Last active March 12, 2017 16:17
Show Gist options
  • Select an option

  • Save ffr4nz/1866d2bc8c5befbefb34ab37cf4b0d50 to your computer and use it in GitHub Desktop.

Select an option

Save ffr4nz/1866d2bc8c5befbefb34ab37cf4b0d50 to your computer and use it in GitHub Desktop.

Revisions

  1. ffr4nz revised this gist Jan 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion json_to_csv_using_stringio.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    csv_doc_field = ['key1','key2'] # Field to be processed
    csv_doc = StringIO.StringIO() # 'False' file to use with CSV Writer
    json_doc = json.loads(json_doc) # Create JSON Object
    output = csv.writer(csv_doc) # CSV writer creation using StringIO file
    output = csv.writer(csv_doc) # Create CSV writer using StringIO file

    tt = list() # Field list
    for field in csv_doc_field: # Loop each field
  2. ffr4nz revised this gist Jan 24, 2017. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions json_to_csv_using_stringio.py
    Original file line number Diff line number Diff line change
    @@ -7,15 +7,15 @@
    '''

    json_doc = '{"key1":"value1","key2":"value2","key3":"value3"}' # JSON Document as Text
    csv_doc_field = ['key1','key2'] # Field to be processed
    csv_doc = StringIO.StringIO() # 'False' file to use with CSV Writer
    json_doc = json.loads(json_doc) # Create JSON Object
    output = csv.writer(csv_doc) # CSV writer creation using StringIO file
    csv_doc_field = ['key1','key2'] # Field to be processed
    csv_doc = StringIO.StringIO() # 'False' file to use with CSV Writer
    json_doc = json.loads(json_doc) # Create JSON Object
    output = csv.writer(csv_doc) # CSV writer creation using StringIO file

    tt = list() # Field list
    for field in csv_doc_field: # Loop each field
    tt.append(json_doc[field]) # Append each value processed
    tt = list() # Field list
    for field in csv_doc_field: # Loop each field
    tt.append(json_doc[field]) # Append each value processed

    output.writerow(tt) # Write row into StringIO file
    print csv_doc.getvalue() # Show result -> value1,value2
    csv_doc.close() # Close StringIO file
    output.writerow(tt) # Write row into StringIO file
    print csv_doc.getvalue() # Show result -> value1,value2
    csv_doc.close() # Close StringIO file
  3. ffr4nz revised this gist Jan 24, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions json_to_csv_using_stringio.py
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,10 @@
    json_doc = json.loads(json_doc) # Create JSON Object
    output = csv.writer(csv_doc) # CSV writer creation using StringIO file

    tt = list() # Field list
    for field in csv_doc_field: # Loop each field
    tt.append(json_doc[field]) # Append each value processed
    tt = list() # Field list
    for field in csv_doc_field: # Loop each field
    tt.append(json_doc[field]) # Append each value processed

    output.writerow(tt) # Write row into StringIO file
    print csv_doc.getvalue() # Show result -> value1,value2
    csv_doc.close() # Close StringIO file
    output.writerow(tt) # Write row into StringIO file
    print csv_doc.getvalue() # Show result -> value1,value2
    csv_doc.close() # Close StringIO file
  4. ffr4nz created this gist Jan 24, 2017.
    21 changes: 21 additions & 0 deletions json_to_csv_using_stringio.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import csv
    import json
    import StringIO

    '''
    Create CSV row using CSV library from JSON Object using StringIO
    '''

    json_doc = '{"key1":"value1","key2":"value2","key3":"value3"}' # JSON Document as Text
    csv_doc_field = ['key1','key2'] # Field to be processed
    csv_doc = StringIO.StringIO() # 'False' file to use with CSV Writer
    json_doc = json.loads(json_doc) # Create JSON Object
    output = csv.writer(csv_doc) # CSV writer creation using StringIO file

    tt = list() # Field list
    for field in csv_doc_field: # Loop each field
    tt.append(json_doc[field]) # Append each value processed

    output.writerow(tt) # Write row into StringIO file
    print csv_doc.getvalue() # Show result -> value1,value2
    csv_doc.close() # Close StringIO file