Hello *,
I'm using Python and the cgi module to retrive data from a HTML form.
I'm then trying to get that information into a string. But efforts fail when
I run type(foo) on my string. type() returns tuple.
My codes follows:
#!/usr/local/bin/python
import cgi
import time
import SeAnRe
# Begin
form = cgi.FieldStorage() # Grab the data from web page form
qu = "("
for name in form.keys():
qu += "'" + str((form[name].value)) + "',"
# Now we remove the 'Submit' text added by form key above and replace it
with it with the date, time and a closing ")"
tail = "'" + str(time.strftime("%Y-%m-%d")) + "','" +
str(time.strftime("%H:%M:%S")) + "')"
final_qu = SeAnRe.Action("'Submit',", tail, qu)
So basicly final_qu would be ('1','two','hello','2003-08-14','23:32:07')
However has stated above type(final_qu) return tuple.
I did get a little advice on running str on every element of final_qu like
this:
foo = ""
for k in final_qu.get_each_value:
foo += str(k)
But then I get "AttributeError: 'tuple' object has no attribute
'get_each_value"
The reason I need foo to be a string is because I'm using pgdb. A Python
interface to PostgreSQL.
Any would be great.
Thanks in advance
-Al 4 9164
"Alastair G. Hogge" <ag*@tpg.com.au> wrote in message
news:3f******@dnews.tpgi.com.au... So basicly final_qu would be ('1','two','hello','2003-08-14','23:32:07') However has stated above type(final_qu) return tuple.
If final_qu is already a tuple of strings, and what you need is one string
with each string in the tuple concatenated, then you can do this: final_qu = ('1','two','hello','2003-08-14','23:32:07') final_qu_str = ' '.join(final_qu) final_qu_str
'1 two hello 2003-08-14 23:32:07'
If you can't guarantee that each tuple element is a string, try this: final_qu_str = ' '.join([str(e) for e in final_qu]) final_qu_str
'1 two hello 2003-08-14 23:32:07'
[str(e) for e in final_qu] creates a list of a strings from the elements in
final_qu.
HTH
Sean
Sean Ross wrote: "Alastair G. Hogge" <ag*@tpg.com.au> wrote in message news:3f******@dnews.tpgi.com.au... So basicly final_qu would be ('1','two','hello','2003-08-14','23:32:07') However has stated above type(final_qu) return tuple.
If final_qu is already a tuple of strings, and what you need is one string with each string in the tuple concatenated, then you can do this:
final_qu = ('1','two','hello','2003-08-14','23:32:07') final_qu_str = ' '.join(final_qu) final_qu_str '1 two hello 2003-08-14 23:32:07' If you can't guarantee that each tuple element is a string, try this: final_qu_str = ' '.join([str(e) for e in final_qu]) final_qu_str '1 two hello 2003-08-14 23:32:07'
[str(e) for e in final_qu] creates a list of a strings from the elements [in final_qu.
HTH Sean
OK. Sorry I didn't quite make myslef clear.
final_qu is a string for pydb. The string should be something link:
INSERT INTO foo VALUES "('1','two','hello','2003-08-14','23:32:07')"
I need the () and each value must be inclosed in '' for the database
interface.
"Alastair G. Hogge" <ag*@tpg.com.au> wrote in message
news:3f******@dnews.tpgi.com.au... Hello *,
I'm using Python and the cgi module to retrive data from a HTML form. I'm then trying to get that information into a string. But efforts fail
when I run type(foo) on my string. type() returns tuple.
At a guess, the problem is in the SeAnRe package. I suspect that
it's using eval() on the input, which will detect the parenthesis and
convert the input into a tuple.
This is, by the way, a *very bad thing to do*, because it can lead
to interesting results when you feed it unchecked data. At least,
they're interesting if you're interested in breaking (or breaking into)
a system.
So the short answer is to not put parenthesis around the string.
The long answer is to use a different package; one that doesn't
use eval(), exec or equivalent.
HTH
John Roth My codes follows: #!/usr/local/bin/python
import cgi import time import SeAnRe
# Begin form = cgi.FieldStorage() # Grab the data from web page form
qu = "(" for name in form.keys(): qu += "'" + str((form[name].value)) + "',"
# Now we remove the 'Submit' text added by form key above and replace it with it with the date, time and a closing ")" tail = "'" + str(time.strftime("%Y-%m-%d")) + "','" + str(time.strftime("%H:%M:%S")) + "')" final_qu = SeAnRe.Action("'Submit',", tail, qu)
So basicly final_qu would be ('1','two','hello','2003-08-14','23:32:07') However has stated above type(final_qu) return tuple. I did get a little advice on running str on every element of final_qu like this: foo = "" for k in final_qu.get_each_value: foo += str(k)
But then I get "AttributeError: 'tuple' object has no attribute 'get_each_value"
The reason I need foo to be a string is because I'm using pgdb. A Python interface to PostgreSQL.
Any would be great. Thanks in advance -Al
Heather Coppersmith wrote: On Sun, 17 Aug 2003 12:37:19 +1000, "Alastair G. Hogge" <ag*@tpg.com.au> wrote:
Sean Ross wrote: "Alastair G. Hogge" <ag*@tpg.com.au> wrote in message news:3f******@dnews.tpgi.com.au... So basicly final_qu would be ('1','two','hello','2003-08-14','23:32:07') However has stated above type(final_qu) return tuple.
If final_qu is already a tuple of strings, and what you need is one string with each string in the tuple concatenated, then you can do this:
>> final_qu = ('1','two','hello','2003-08-14','23:32:07') >> final_qu_str = ' '.join(final_qu) >> final_qu_str '1 two hello 2003-08-14 23:32:07' >>
If you can't guarantee that each tuple element is a string, try this: >> final_qu_str = ' '.join([str(e) for e in final_qu]) >> final_qu_str '1 two hello 2003-08-14 23:32:07' >>
[str(e) for e in final_qu] creates a list of a strings from the elements [in final_qu.
HTH Sean
OK. Sorry I didn't quite make myslef clear. final_qu is a string for pydb. The string should be something link: INSERT INTO foo VALUES "('1','two','hello','2003-08-14','23:32:07')" I need the () and each value must be inclosed in '' for the database interface.
Having just been through this myself, you're playing with fire: what if one of those strings contains a quote character (single or double)?
Let the database interface do the quoting for you. I don't know what your table looks like, but I see two possibilities (all code untested):
If your table contains a single column in which you're trying to put that whole string:
sql = """INSERT INTO foo VALUES (%s)""" params = (repr( final_qu ),) # "str" may work here, too pydbcursor.execute( sql, params )
If your table contains five columns, one for each element of that tuple:
sql = """INSERT INTO foo VALUES (""" \ + ','.join( ['%s'] * len( final_qu ) ) \ + """)""" params = final_qu pydbcursor.execute( sql, params )
HTH, Heather
OK I removed the all the 's from the string.
But now I get this:
<error>
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql ), referer: http://nova/~agh/house.html
pgdb.DatabaseError: error 'ERROR: parser: parse error at or near ":" at
character 155, referer: http://nova/~agh/house.html
' in 'INSERT INTO house VALUES
(1,two,three,four,five,six,seven,eight,nine,ten,el even,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26, 27,28,29,30,N/A,2003-08-18,13:19:06)',
referer: http://nova/~agh/house.html
</error>
As my modifed code:
<code>
#!/usr/local/bin/python
import cgi
import time
import pgdb
def Initialise():
_dbuser = "agh"
_dbsource = "127.0.0.1:foo" # dsn.
_dbname = "foo"
return (pgdb.connect(dsn=_dbsource, user=_dbuser, database=_dbname))
def AddData(query):
db = Initialise()
cur = db.cursor()
cur.execute(query)
db.commit()
cur.close()
db.close()
# Begin
form = cgi.FieldStorage() # Grab the data from web page form
qu = ""
# Now we put all the form data into one big string for the db query.
for name in form.keys():
if form[name].value == "Submit":
qu += str(time.strftime("%Y-%m-%d")) + "," +
str(time.strftime("%H:%M:%S"))
break
else:
qu += form[name].value + ","
StoreData.AddData("INSERT INTO house VALUES (%s)" % (qu))
DisplayContent.WebPage(qu)
</code> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Lukas Kasprowicz |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi Folks,
My Proglem is, I get after a query on a mysql database with module MySQLdb a
tuple but I need this output from database as a string....
|
by: Steve M |
last post by:
Hello,
I'm trying to figure out the index position of a tuple member.
I know the member name, but I need to know the members index position. I
know that if I use the statement print tuple that...
|
by: Kay Schluehr |
last post by:
Hi all,
thanks for Your attention !
I think my proposal was more in mind of Rons modified exec than
Pythons lambda.
When George proposed his unpacking behavoir for list-comps as a pack of...
|
by: flyaflya |
last post by:
a = "(1,2,3)"
I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
'2', ',', '3', ')') not (1,2,3)
|
by: Carl J. Van Arsdall |
last post by:
From my interpreter prompt:
>>> tuple = ("blah")
>>> len(tuple)
4
>>> tuple2 = ("blah",)
>>> len (tuple2)
1
So why is a tuple containing the string "blah" without the comma of
|
by: localpricemaps |
last post by:
i am having a problem writing a tuple to a text file. my code is
below.
what i end up getting is a text file that looks like this
burger, 7up
burger, 7up
burger, 7up
and this is instead...
|
by: ronrsr |
last post by:
here is my result.
How do I determine the number of tuples in this array, returned from a
mysql database?
How do I determine the number of characters or entry in each tuple?
thanks very much...
|
by: Daniel |
last post by:
I have a list of strings, which I need to convert into tuples. If the
string is not in python tuple format (i.e. "('one', 'two')", "("one",
'two')", etc.), then I can just make it a 1-tuple (i.e....
|
by: TP |
last post by:
Hi everybody,
I have a question about the difference of behavior of "len" when applied on
tuples or on lists. I mean:
$ len( ( 'foo', 'bar' ) )
2
$ len( ( 'foo' ) )
3
$ len( )
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |