473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

tokenise a string

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
--
Matthias Teege -- http://www.mteege.de
make world not war
Jul 18 '05 #1
3 1972
On 2004-10-09, Matthias Teege <ma************@mteege.de> wrote:
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 ....
--
Cousin Stanley
Human Being
Phoenix, Arizona
Jul 18 '05 #2
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 =)

Cousin Stanley <co***********@hotmail.com> wrote in message news:<2s*************@uni-berlin.de>...
On 2004-10-09, Matthias Teege <ma************@mteege.de> wrote:
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 ....

Jul 18 '05 #3
Matthias Teege <ma************@mteege.de> wrote in message
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#
import shlex
import cStringIO
myFilelikeString = cStringIO.StringIO("name=matthias;count>10")
lexer = shlex.shlex(myFilelikeString)
while 1:

.... 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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
1
by: jason.teen | last post by:
Hi, I'm just wondering if something like this is possible to either write queries or VBA code to do. I need to tokenise the and then flatten out this file. tblBefore...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.