将字典解析为csv文件

时间:2016-12-08 04:54:14

标签: python json parsing dictionary

从这些词典中过滤掉特定键然后写入csv文件的最佳方法是什么?

这些数据存储在一个文本文件中,并具有多个这些数据。这只是数据的一部分。

   var formattedObject = makeLinksObject();

   formattedObject
   .done(function(renderedObject) {
     render(renderObject);
   })

   function makeLinksObject() {
     var dfd = getLastTimeUpdated();
     var linksArray = [];
     var linksObject = {};

     return dfd.then(function(dateUpdated) {
       $.each(links, function(index, value) {
         var linkObject = {};
         obj.Title = value.Title.toLowerCase();
         linksArray.push(obj);
       });

       linksObject = {
         lblcallerId: "some value here",
         links: linksArray
       }

     })
     .then(function() {
       return linksObject
     });
   }

   function getLastTimeUpdated() {
     var modificationUrl = "serverurl"
     return $.ajax({
       url: modificationUrl,
       method: "GET",
       headers: {
         "accept": "application/json;odata=verbose"
       }
     })
     .then(function(data) {
       return data.d.LastItemModified;
     })

   }

3 个答案:

答案 0 :(得分:1)

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)

答案 1 :(得分:1)

在这种情况下,

$( document ).ready(function() { // Traverse throw all rows $('.student_marks tbody tr').each(function(){ // Get current row var student = $(this); var total_points = 0; $(student).find('.marks').each(function(){ total_points+=parseInt($(this).val()); }) $(student).find('.total').html(total_points); }) });对我来说似乎更优雅

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
   <table class="table student_marks" >
    <thead>
      <tr>
        <th>Student Name</th>
        <th>Math</th>
        <th>History</th>
        <th>Total</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="name">John</td>
        <td><input value="50" readonly class="marks"/></td>
        <td><input value="50" readonly class="marks"/></td>
        <td class="total"></td>
      </tr>
      <tr>
        <td class="name">Mac</td>
        <td><input value="60" readonly class="marks"/></td>
        <td><input value="50" readonly class="marks"/></td>
        <td class="total"></td>
      </tr>
      <tr>
        <td class="name">Sena</td>
        <td><input value="40" readonly class="marks"/></td>
        <td><input value="70" readonly class="marks"/></td>
        <td class="total"></td>
      </tr>
      <tr>
        <td class="name">Devy</td>
        <td><input value="80" readonly class="marks"/></td>
        <td><input value="90" readonly class="marks"/></td>
        <td class="total"></td>
      </tr>
     
    </tbody>
  </table>
</div>

答案 2 :(得分:0)

您可以使用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])