Skip to content

Instantly share code, notes, and snippets.

@athoune
Created August 29, 2016 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athoune/ea3c6cd96838e7202a490b77301de20c to your computer and use it in GitHub Desktop.
Save athoune/ea3c6cd96838e7202a490b77301de20c to your computer and use it in GitHub Desktop.
Parsing influxdb text format with pyparsing
from pyparsing import QuotedString, Word, nums, delimitedList, Optional, \
Regex, Group, Suppress, ParseException
integer = Regex(r"(?P<integer>\d+)i").setParseAction(lambda s, locs, toks:
int(toks[0][:-1]))
real = Regex(r"[+-]?\d+(\.\d+)?")("real").setParseAction(lambda s, locs, toks:
float(toks[0]))
number = Word(nums)
key = Regex(r"[a-zA-Z][a-zA-Z0-9_]*")
quoted = QuotedString('"')
blob = Regex(r"[^ ,]+")
LINE = key("key") + Optional(Suppress(",") + Group(delimitedList(Group(key +
Suppress("=")
+ (blob ^ quoted))))("tags")) +\
Group(delimitedList(Group(key + Suppress("=") + (integer ^ real ^ quoted))))("values") +\
number("ts")
def readflux(reader):
for line in reader:
try:
l = LINE.parseString(line)
except ParseException as p:
print(line)
print(" " * p.col + "^")
raise p
else:
yield l['key'], int(l['ts']),\
dict((a[0], a[1]) for a in l['values']),\
dict((a[0], a[1]) for a in l['tags'])
if __name__ == '__main__':
import sys
for line in readflux(open(sys.argv[1], 'r')):
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment