1

几乎肯定有人问过这个问题。

我在文件中有timeseries看起来像这样的.csv数据。数据没有标题,但这是不同字段的内容: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

我正在尝试将此数据导入InfluxDBfrom python。我在文档中看到您必须json逐行创建它,但我不确定这是如何完成的:

这很接近,但没有雪茄。假设我已经拆分了字符串并且日期时间时间戳已经采用了正确的格式。当我打印下面的 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 )
4

1 回答 1

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]
 }
于 2017-09-24T01:36:26.477 回答