代码片段收集,整理

1.把16进制字符串转换为二进制写入到文件中。

 with open(fileName, 'wb')as fp:
        for x in list_dec:
            a = struct.pack('B', int(x, 16))
            fp.write(a)
        fp.close()

2.读取二进制数据,把short占两个字节二进制数据转为整数。前5个byte表示的是个数,后11个byte表示的是整数,还原数据。

def reading(content):
    data = []
    for num in range(0, int((len(content)) / 2)):
        count = int('{:08b}'.format(content[num * 2])[0:5], 2)
        d = int(('{:08b}'.format(content[num * 2]) + '{:08b}'.format(content[num * 2 + 1]))[-11:], 2) * 2
        for n in range(0, count):
            data.append(d)
        
    return data

3.javascript前端实现把elasticsearch查询到的数据,转换格式。

function f(json) {

    var myObj = {
        "took": 5,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 44,
                "relation": "eq"
            },
            "max_score": 5.500709,
            "hits": [
                {
                    "_index": "wsnevent",
                    "_type": "_doc",
                    "_id": "1564934950000-wsntransient-5a6eb4e70b272a1f64fa26b6-pressure",
                    "_score": 5.500709,
                    "_source": {
                        "eventid": "1564934950000-wsntransient-5a6eb4e70b272a1f64fa26b6-pressure",
                        "confidence": "22.440850817799575",
                        "ts": 1564934956000
                    }
                },
                {
                    "_index": "wsnevent",
                    "_type": "_doc",
                    "_id": "1564935570000-wsntransient-5a6eb4e70b272a1f64fa26b3-pressure",
                    "_score": 5.500709,
                    "_source": {
                        "eventid": "1564935570000-wsntransient-5a6eb4e70b272a1f64fa26b3-pressure",
                        "confidence": "22.249483979470742",
                        "ts": 1564935570000
                    }
                }}
         ]
        }
    }
    myObj = JSON.stringify(myObj)  //转换为字符串
    var myJSON = JSON.parse(myObj);  //转换为json对象

    var datas = myJSON.hits.hits
    var scatterData = {1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [], 11: []} //待转换的格式
    var siteArr = new Array('','22','33','44','55','66','77','788','889','00','88778')

    for (var data in datas) {
        var eventid = datas[data]._source.eventid
        var y = datas[data]._source.confidence
        var x = datas[data]._source.ts
        var siteid = eventid.split('-')[2]
        var index = siteArr.indexOf(siteid)
        var obj = {}
        obj['x']=x
        obj['y'] = y
        obj['eventid'] = eventid
        scatterData[index].push(obj)

    }

    return scatterData

}

 

你可能感兴趣的:(代码片段收集,整理)