Bugazelle / export-csv-to-influx

The python library to write the CSV data into Influx. Support me via Bitcoin: bc1qqgkmph9cvygzxfpupv4jr4n0nfx3qumwg39j5w
BSD 3-Clause "New" or "Revised" License
35 stars 10 forks source link

exporting 100+ tags into influxdb #16

Closed shnabin closed 4 years ago

shnabin commented 4 years ago

Hi @Bugazelle ,

I'm trying to read a csv into influxdb with almost 100+ tags, i read the tags into a variable and tried using that variable against the tag_columns but its not working, I'm not sure how to pass these many tags. Is there a way to pass these tags to the tags_columns instead of manually entering them.

from ExportCsvToInflux import ExporterObject from influxdb import InfluxDBClient exporter = ExporterObject() import pandas as pd path = r'G:\data.csv' df = pd.read_csv(path) par=df.columns[2:104] print (par) exporter.export_csv_to_influx (csv_file =r'G:\data.csv' db_name= 'mydb', db_measurement= 'demo', time_column= 'Time', tag_columns= par, field_columns= 'Blo', db_user= 'admin', db_password= '', force_insert_even_csv_no_update= True, db_server_name= 'localhost:8086', time_format='%H:%M')

The output is as follows:

Index(['dat1', 'dat2', 'dat3', 'dat4', 'dat5', 'dat6', 'dat7', 'dat8', 'dat9', 'dat10', ... 'dat93', 'dat94', 'dat95', 'dat96', 'dat97', 'dat98', 'dat99', 'dat100', 'dat101', 'dat102'], dtype='object', length=102) Traceback (most recent call last): File "<pyshell#17>", line 11, in time_format='%H:%M') File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ExportCsvToInflux\exporter_object.py", line 175, in export_csv_to_influx field_columns = base_object.str_to_list(field_columns) File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ExportCsvToInflux\base_object.py", line 33, in str_to_list elif bool(string) is False: File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 2387, in nonzero self.class.name ValueError: The truth value of a Index is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

My csv is as follows:

Time Blo dat1 dat2 dat3 dat4 dat5 dat6 dat7 dat8 dat9 dat10 dat11 dat12 dat13 dat14 dat15
06:08 rain 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
06:09 rain 0 0 0 0 0 0.001 0 0 0 0 0 0 0 0 0
06:10 rain 0 0 0 0 0 0.015 0 0 0 0 0 0.003 0 0 0
06:11 rain 0 0 0 0 0.013 0.068 0 0 0 0 0 0.024 0 0 0
06:12 rain 0 0 0 0 0.015 0.064 0 0.019 0.027 0 0 0.044 0 0 0.016
06:13 rain 0.022 0.001 0 0 0.047 0.074 0.002 0.023 0.048 0 0.046 0.064 0 0.016 0.036
06:14 rain 0.055 0.011 0 0 0.051 0.084 0.013 0.045 0.058 0.001 0.021 0.086 0.032 0.037 0.057
06:15 rain 0.065 0.041 0.014 0 0.076 0.074 0.039 0.075 0.081 0.036 0.058 0.111 0.053 0.068 0.082
06:16 rain 0.109 0.075 0.034 0.025 0.103 0.137 0.073 0.101 0.119 0.054 0.098 0.146 0.086 0.091 0.116

Thank you for your support.

Bugazelle commented 4 years ago

Hello @krishnacy

Sorry for the delay.

Your par is not list type nor str type. The par is the <class 'pandas.core.indexes.base.Index'>

Consider you use the pandas, suggest to convert to list as following.

par = list(par)

Here is the full code:

from ExportCsvToInflux import ExporterObject
import pandas as pd

exporter = ExporterObject()
path = r'data.csv'
df = pd.read_csv(path)
par = df.columns[2:104]
print(type(par))
print(par)
par = list(par)
print(par)

exporter.export_csv_to_influx(csv_file=r'data.csv',
                              db_name='mydb',
                              db_measurement='demo',
                              time_column='Time',
                              tag_columns=par,
                              field_columns='Blo',
                              db_user='admin',
                              db_password='',
                              force_insert_even_csv_no_update=True,
                              db_server_name='localhost:8086',
                              time_format='%H:%M')
shnabin commented 4 years ago

Hi @Bugazelle

Thank you for the support. Sorry for the delay in replying. Works perfectly now!!!

Cheers!!