James
James

Reputation: 305

parsing dictionaries into csv file

What are the best ways to filter out specific keys from these dictionaries then write into csv file?

These data are stored in a text file and have multiple of these. This is just a portion of data.

{u'decrypted': True, u'fcnt': 3, u'timestamp': u'2016-11-30T17:50:00.533Z', u'dataFrame': u'AB3hqqqpVVVOAAA=', u'id': 1480528200533L, u'sf_used': 10, u'snr': -8.5, u'rssi': -116, u'port': 5}
{u'decrypted': True, u'fcnt': 5, u'timestamp': u'2016-11-30T17:50:35.613Z', u'dataFrame': u'AB3hqqqpVVVOAAA=', u'id': 1480528235613L, u'sf_used': 10, u'snr': -5.8, u'rssi': -119, u'port': 5}
{u'decrypted': True, u'fcnt': 7, u'timestamp': u'2016-11-30T17:51:50.609Z', u'dataFrame': u'AB7hqqqpVVVOAAA=', u'id': 1480528310609L, u'sf_used': 10, u'snr': -8.8, u'rssi': -120, u'port': 5}
{u'decrypted': True, u'fcnt': 9, u'timestamp': u'2016-11-30T17:53:23.504Z', u'dataFrame': u'AB7hqqqpVVVOAAA=', u'id': 1480528403504L, u'sf_used': 10, u'snr': -9.2, u'rssi': -116, u'port': 5}
{u'decrypted': True, u'fcnt': 13, u'timestamp': u'2016-11-30T17:55:25.060Z', u'dataFrame': u'AB7iqqqpVVVOAAA=', u'id': 1480528525060L, u'sf_used': 10, u'snr': -8.5, u'rssi': -111, u'port': 5}
{u'decrypted': True, u'fcnt': 15, u'timestamp': u'2016-11-30T17:56:48.140Z', u'dataFrame': u'AB7iqqqpVVVOAAA=', u'id': 1480528608140L, u'sf_used': 10, u'snr': -8.2, u'rssi': -110, u'port': 5}

Upvotes: 1

Views: 71

Answers (3)

Hussain
Hussain

Reputation: 5187

DictWriter seems more elegant for me in this case

import ast
import csv

with open('input.txt', 'r') as in_f, open('data.csv', 'w') as out_f:
    data = in_f.readlines()

    # Take specific fields and ignore others
    writer = csv.DictWriter(out_f, fieldnames=['id', 'dataFrames', 'timestamp'], extrasaction='ignore')
    writer.writeheader()  # For writing header

    for row in data:
        dict_row = ast.literal_eval(row)  # because row has a dict string
        writer.writerow(dict_row)

Upvotes: 1

Will
Will

Reputation: 4469

You can use csv.writer

with open('data.csv', 'w+') as csv_file:
    writer = csv.writer(csv_file)
    for key, value in my_dict.iteritems():
       if key in ["timestamp", "dataFrame"]:
           writer.writerow([key, value])

Upvotes: 0

Thmei Esi
Thmei Esi

Reputation: 442

data = [
{u'decrypted': True, u'fcnt': 3, u'timestamp': u'2016-11-30T17:50:00.533Z', u'dataFrame': u'AB3hqqqpVVVOAAA=', u'id': 1480528200533, u'sf_used': 10, u'snr': -8.5, u'rssi': -116, u'port': 5},
{u'decrypted': True, u'fcnt': 5, u'timestamp': u'2016-11-30T17:50:35.613Z', u'dataFrame': u'AB3hqqqpVVVOAAA=', u'id': 1480528235613, u'sf_used': 10, u'snr': -5.8, u'rssi': -119, u'port': 5},
{u'decrypted': True, u'fcnt': 7, u'timestamp': u'2016-11-30T17:51:50.609Z', u'dataFrame': u'AB7hqqqpVVVOAAA=', u'id': 1480528310609, u'sf_used': 10, u'snr': -8.8, u'rssi': -120, u'port': 5},
{u'decrypted': True, u'fcnt': 9, u'timestamp': u'2016-11-30T17:53:23.504Z', u'dataFrame': u'AB7hqqqpVVVOAAA=', u'id': 1480528403504, u'sf_used': 10, u'snr': -9.2, u'rssi': -116, u'port': 5},
{u'decrypted': True, u'fcnt': 13, u'timestamp': u'2016-11-30T17:55:25.060Z', u'dataFrame': u'AB7iqqqpVVVOAAA=', u'id': 1480528525060, u'sf_used': 10, u'snr': -8.5, u'rssi': -111, u'port': 5},
{u'decrypted': True, u'fcnt': 15, u'timestamp': u'2016-11-30T17:56:48.140Z', u'dataFrame': u'AB7iqqqpVVVOAAA=', u'id': 1480528608140, u'sf_used': 10, u'snr': -8.2, u'rssi': -110, u'port': 5}
]
filt = ['dataFrame', 'timestamp']
with open('myfile.csv', 'w') as outfile:
    for row in data:
        values = [str(row[i]) for i in filt]
        print(', '.join(values), file=outfile)

Upvotes: 1

Related Questions