2017-09-23 95 views
0

這幾乎肯定會被問到。從python將csv數據導入influxdb

我在.csv文件中有timeseries這樣的數據。該數據沒有標題,但是這是在不同的領域有:date,time,open,high,low,close,volume,0

2017-09-22,21:50:00,131.36,131.415,131.35,131.39,78489,0 
2017-09-22,21:55:00,131.39,131.4,131.37,131.38,95322,0 
2017-09-22,22:00:00,131.38,131.6,131.31,131.39,1212804,0 

我試圖將此數據從python導入InfluxDB。我在文檔中看到,您必須爲它逐行創建json,但我不確定這是如何完成的:

這很接近,但沒有雪茄。假設我已經將字符串拆分並且datetime-timestamp已經採用了正確的格式。當我打印下面的json_body時,我得到例如:

[{'fields': {'Int_value': '1212804', 'Float_value': '131.39'}, 'time': datetime.datetime(2017, 9, 22, 22, 0), 'measurement': 'quote'}] 

這似乎不正確 - 我似乎只得到一個浮點值。

輸入到InfluxDB的正確JSON是什麼?

import datetime 
import random 
import time 
import os 
import csv 
from csv import reader 
import argparse 
from influxdb import client as influxdb 


db = influxdb.InfluxDBClient("127.0.0.1", 8086, "", "", "stocks") 


def read_data(filename): 
    print filename 
    with open(filename) as f: 
     lines = f.readlines()[1:] 

    return lines 

if __name__ == '__main__': 
    filename = r"jnj.us.csv" 
    lines = read_data(filename) 
    for rawline in lines: 
     line = rawline.split(",") 
     d= getPythonDateTimeFromStr(line[0], line[1]) 
     #EVERYTHING UP TO HERE WORKS. Not sure how to create the json below 
     #==================================== 
     json_body = [ 
     { 
      "measurement": "quote", 
      "time": d, 
      "fields": { 
       "Float_value": line[2], 
       "Float_value": line[3], 
       "Float_value": line[4], 
       "Float_value": line[5], 
       "Int_value": line[6] 
      } 
     } 
     ] 

     print json_body 

     db.write_points(json_body) 

回答

1

咄,

"fields": { 
      "Float_value": line[2], 
      "Float_value": line[3], 
      "Float_value": line[4], 
      "Float_value": line[5], 
      "Int_value": line[6] 
} 

應該

"fields": { 
      "Open": line[2], 
      "High": line[3], 
      "Low": line[4], 
      "Close": line[5], 
      "Volume": line[6] 
}