从这些字典中过滤掉特定键然后写入 csv 文件的最佳方法是什么?
这些数据存储在一个文本文件中,并且有多个。这只是数据的一部分。
{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}
最佳答案
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)
关于python - 将字典解析为 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41032027/