tokenise a string

M

Matthias Teege

Moin,

I'm new to python. I try to build a "easy" search interface to a
database table in python. I have a input string like this:
"name=matthias;count>10" or this "location!=thisone". From this input
I try to build a SQL query like this: select * from tbl where
nme='matthias' and cnt > 10.

So I must parse the input, build tokens and map the fieldnames. Is
there a special modul which I can use or are the standard string
functions adequate?

Many thanks
Matthias
 
C

Cousin Stanley

Moin,
....
So I must parse the input, build tokens and map the fieldnames.

Is there a special modul which I can use
or are the standard string functions adequate?

Matthias ....

You might check the tokenize module ....

import tokenize

help( tokenize )

I only know of it, but have never used it myself ....
 
L

Lonnie Princehouse

The tokenize module is only useful for parsing Python code. That
said, reading the code of tokenize.py might be helpful in writing your
own tokenizer.

Look into the re (regular expression) module, or PLY or Spark if you
need more sophisticated parsers.

Also check out SQLObject (http://sqlobject.org/) --- it makes SQL
almost pleasant =)
 
M

M.E.Farmer

Matthias Teege said:
I have a input string like this:
"name=matthias;count>10" or this "location!=thisone".
So I must parse the input, build tokens and map the fieldnames. Is
there a special modul which I can use or are the standard string
functions adequate?

Many thanks
Matthias

This might be helpful.
Shlex can do much more, but I have not had the time to explore it.
#python shell#.... token = lexer.get_token()
.... if token:
.... print token
.... else:
.... break
....
name
=
matthias
;
count
Also look at PySourceColor that I posted it covers token and tokenize.
HTH,
M.E.Farmer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top