Pilven Matthieu
Pilven Matthieu

Reputation: 77

Make python consider a quoted string as a block

I'm making a grammar using Pyparsing and want to make it parse all the inserts, even those having a date. I don't get how to make the quotedStrings an entire block.

ident          = Word( alphas, alphanums + "_$" ).setName("identifier")
number         = Word(nums)

quotedString = "'" + delimitedList(number | ident , ".", combine=True) + "'"
quotedStringList = Group(delimitedList(quotedString))

insert_statement <<= (INSERT + INTO + tableNameList + VALUES + delimitedList(openingparenthese + quotedStringList + closingparenthese) + Optional(endLine))

data = "INSERT INTO test VALUES('2008-07-28 00:00:05', 'a');"
print (data, "\n", insert_statement.parseString( data ))

I expect to get something like :

['insert', 'into', ['test'], 'values', '(', ["'", '2008-07-28 00:00:05', "'", "'", 'a', "'"], ')', ';']

But I only get :

Expected "'"

Upvotes: 4

Views: 80

Answers (2)

Pilven Matthieu
Pilven Matthieu

Reputation: 77

I don't think this mistake will happen to many people, but the easiest way to have quoted Strings...is to use the one given by pyparser from pyparser import quotedString, ... as pp instead of trying to create your own

Thanks for the replies anyway, helped me for other problems

Upvotes: 1

Michele Rava
Michele Rava

Reputation: 304

I cannot comment so I must create an answer Try to use the str(text).replace ("'", "\'") Method or with regex

import re
re.sub(re.compile("'"),"\'", str(text))

I'm not at keyboard, so be patient. But I hope it can help someway

Upvotes: 2

Related Questions