使用Python在三个不同的站点中读取三组数据

时间:2017-06-02 08:54:42

标签: python

我有这样的文件(data.txt):

每列包含来自3个不同站点的数据(1,2,3..1,2,3..so),所以现在我想读取每个站点1的数据(第1行,第4行,第4行) 7),站2(第2行,第5行,第8行)和第3站(第3行,第6行,第9行)....

我该怎么办?如果有人以前曾经问过,我的意见。

由于

2 个答案:

答案 0 :(得分:3)

如果您的数据以逗号分隔,则可以使用:

with open('data.txt') as d:
    data = [list(map(float, line.split(','))) for line in d]
    station1 = data[::3] #from first with step 3
    station2 = data[1::3] #from second with step 3
    station3 = data[2::3] #from third with step 3
    print station1
    print station2
    print station3

但是,如果对其值使用其他分隔符更改split()参数。

修改

您必须使用Python 3.x,因此您需要将地图转换为列表。在3.x map()中返回迭代器。

答案 1 :(得分:1)

//  1. Enter sheet name where data is to be written below
        var SHEET_NAME = "Form Responses 1";

//  2. Run > setup
//
//  3. Publish > Deploy as web app
//    - enter Project Version name and click 'Save New Version'
//    - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously)
//
//  4. Copy the 'Current web app URL' and post this in your form/script action
//
//  5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case)

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service

// If you don't want to expose either GET or POST methods you can comment out the appropriate function


function doPost(e){
  return handleResponse(e);
}

function handleResponse(e) {

  var lock = LockService.getPublicLock();
  lock.waitLock(30000); 

  try {

    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName(SHEET_NAME);


    var headRow = e.parameter.header_row || 1;
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = [];
    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push(e.parameter[headers[i]]);
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
          .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){
    // if error return this
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { //release lock
    lock.releaseLock();
  }
}

function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}