1

I'm trying to use Python to search an InfluxDB database for data closely matching specific coordinates. I have converted all coordinates from lat/lon to plus codes (i.e. 9C4WXRRQ+69). In order to search InfluxDB for codes similar, I need to use RegEx, which requires me to escape the '+' character using a backslash.

When I send a query from Python, it adds a second backslash (which I know Python uses to display a single backslash) which then results in a failed query. Is there any way of sending a string query from Python with only a single backslash or should I stick to the "fudging it" method of removing all '+' signs from my codes and thereby negating the need?

I have tried the methods mentioned on the below questions, along with multiple others, but all send a double backslash.

Why can't Python's raw string literals end with a single backslash?

How to replace a double backslash with a single backslash in python?

In Influx CMD, I can use the following:

SELECT count(olc) FROM OLC WHERE olc =~ /9C4WXRRQ\+69*/

Which returns:

name: OLC
time count
---- -----
0    1104

However, if I send the same query from Python:

influx.query("SELECT count(olc) FROM OLC WHERE olc =~ /9C4WXRRQ\+69*/")

The InfluxDB readout gives the following (which shows the double backslash) and returns a blank response:

2019-01-17T14:20:28.849772Z     info    Executing query {"log_id": "0C~7QHG0000", "service": "query", "query": "SELECT count(olc) FROM olctest.autogen.OLC WHERE olc =~ /9C4WXRRQ\\+69*/"}

If I remove all '+' signs from the plus codes, the query works fine:

influx.query("SELECT count(olc) FROM OLC WHERE olc =~ /9C4WXRRQ69*/")

Resulting in:

ResultSet({'('OLC', None)': [{'time': '1970-01-01T00:00:00Z', 'count': 1104}]})

Which shows querying from Python is not the problem.

Tom
  • 11
  • 3
  • I don't know Python, but is there a way to use a binary substitute for the backslash? Maybe? – Pete Jan 17 '19 at 15:15
  • Try this `influx.query("SELECT count(olc) FROM OLC WHERE olc =~ /9C4WXRRQ" + "\\" + "+69*/")` – Pete Jan 17 '19 at 15:25
  • What I can do is replace the '+' with a '.' (regex: any character) when searching the database. This enables the true plus codes to be stored and searched while negating any backslash issue. The double backslash outside of Python still seems like an oversight. – Tom Jan 17 '19 at 15:28
  • 1
    Adding in a double backslash instead of a single backslash seems to work. I'll test it a bit more to confirm. – Tom Jan 17 '19 at 15:31

1 Answers1

-1

you have to use two backslah \\ in the request. one backslash is considered as a special caracter in python

Simon PII
  • 441
  • 3
  • 8