string.join question 
Author Message
 string.join question

Trying to join a string as follows: string.join(['\,'aaa','\],'')
(the last characters are 2 single quotes).
I want a character string which looks like 'aaa' (with quotes). Anybody know
what I'm doing wrong? or if this is possible with string.join?
Thanks.

-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
    http://www.*-*-*.com/       The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----



Mon, 21 Mar 2005 09:47:44 GMT  
 string.join question

Quote:

> Trying to join a string as follows: string.join(['\,'aaa','\],'')
> (the last characters are 2 single quotes).
> I want a character string which looks like 'aaa' (with quotes).
> Anybody know
> what I'm doing wrong? or if this is possible with string.join?

If I understand you correctly (which I'm not sure that I do), try
something like

        string.join(["'", 'aaa', "'"], '')

If you insist on using single quotes, you can use '\'' instead of "'".

--

 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Love is the selfishness of two persons.
\__/ Antoine de la Salle
    Maths reference / http://www.alcyone.com/max/reference/maths/
 A mathematics reference.



Mon, 21 Mar 2005 09:02:25 GMT  
 string.join question
Thanks - it "kind of" works - but not quite. So now there is a part 2 of the
question:

value = 'aaa'
a = string.join(string.join(["'", value,"'"],''),',')

This should yield (what I want.....): 'aaa',

Instead it yields: ',a,a,a,'

??????????????????????? Looks crazy to me......

-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
   http://www.newsfeed.com       The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----



Mon, 21 Mar 2005 10:24:39 GMT  
 string.join question

Quote:
> Thanks - it "kind of" works - but not quite. So now there is a part 2 of the
> question:

> value = 'aaa'
> a = string.join(string.join(["'", value,"'"],''),',')

> This should yield (what I want.....): 'aaa',

> Instead it yields: ',a,a,a,'

> ??????????????????????? Looks crazy to me......

No, it's right, because join takes a sequence. Your "aaa"
string is a sequence of characters. :)

What are you really trying to do? :)

Cheers,


------------------------------------------------------------
Switch to Cornerhost!             http://www.cornerhost.com/
 Low Priced, Reliable Blog Hosting, With a Human Touch. :)
------------------------------------------------------------



Mon, 21 Mar 2005 10:00:42 GMT  
 string.join question
I'm accessing MySQL from Python. This is for an insert statement which looks
like this:

   values = [input1, input2]
# input values are derived from askstring, ex: input1 = aaa, input =bbb
   sqlcmd = sql.Insert_Cmd (tables, columns, values)
-----------------------------------------------------------------------
  def Insert_Cmd(self,tables,fields,values):

    insert_str = 'INSERT INTO %s ( %s ) VALUES ( %s )' %
(string.join(tables,','),string.join(fields,','),string.join(values,','))

This yields: INSERT INTO tablename (column1,column2) VALUES (aaa,bbb)

What I need is:
INSERT INTO tablename (column1,column2) VALUES ('aaa','bbb')

Regards,

Andy

Quote:
-----Original Message-----



Date: Wednesday, October 02, 2002 9:54 PM
Subject: Re: string.join question


>> Thanks - it "kind of" works - but not quite. So now there is a part 2 of
the
>> question:

>> value = 'aaa'
>> a = string.join(string.join(["'", value,"'"],''),',')

>> This should yield (what I want.....): 'aaa',

>> Instead it yields: ',a,a,a,'

>> ??????????????????????? Looks crazy to me......

>No, it's right, because join takes a sequence. Your "aaa"
>string is a sequence of characters. :)

>What are you really trying to do? :)

>Cheers,


>------------------------------------------------------------
>Switch to Cornerhost!             http://www.cornerhost.com/
> Low Priced, Reliable Blog Hosting, With a Human Touch. :)
>------------------------------------------------------------



Mon, 21 Mar 2005 11:28:48 GMT  
 string.join question

Quote:
> I'm accessing MySQL from Python. This is for an insert statement
> which looks like this:

Does your MySQL library have something that does this for you?  Something
like stored procedures in SQL Server?  You want to be careful with user
input so you don't allow something like this:

  last_value = "'mundane'; drop database 'foo';"

Realizing the above is an imperfect expression of a potential exploit and
ignoring the whole issue for now, consider this:

#!/usr/bin/env python

"""
This could easily be improved upon, etc.
"""

def quote(s):
  """Return value wrapped in single-quotation marks."""
  # Ignore the need to escape quotes for now.
  return "'%s'" % s

def insert_sql(tablename, columns, values):
  """Return INSERT statement."""
  sql_template = "insert into %(tablename)s (%(columns)s) values
(%(values)s);"
  columns = ','.join(columns)
  values = ','.join([quote(v) for v in values])
  return sql_template % locals()

tablename = 'foobar'
columns = ['field1', 'field2', 'field3']
values = ['a', 'b', 'c']

print insert_sql(tablename, columns, values)

Cheers,

// m



Mon, 21 Mar 2005 10:45:48 GMT  
 string.join question

Quote:

> I'm accessing MySQL from Python. This is for an insert
> statement which looks like this:

Aha... :) Maybe this will help:

http://cvs.sabren.com/sixthdev/cvsweb.cgi/storage/MySQLStorage.py?rev...

Cheers,


------------------------------------------------------------
Switch to Cornerhost!             http://www.cornerhost.com/
 Low Priced, Reliable Blog Hosting, With a Human Touch. :)
------------------------------------------------------------



Mon, 21 Mar 2005 11:22:53 GMT  
 string.join question

Quote:
> Hmmmm.... I'm going to have to figure this out.
> I can test for a ' or " and respond with an invalid entry remark (which it
> would be...).

  one_possible_likely_value = """He said, "I don't think you really want to
omit quotation marks, do you?" """

<wink>

Quote:
> I'm ok with everything except the statement:
> ([quote(v) for v in values])

Did you notice the function quote() I supplied?

  new_list = [func(x) for x in seq]

is called a list comprehension.  It's just a different way to write this:

  new_list = []
  for x in seq:
    new_list.append(func(x))

Please reply to the list, by the way, since this is all on topic and others
may have more to contribute.

Cheers,

// mark



Mon, 21 Mar 2005 11:19:20 GMT  
 string.join question
Damn you're good!!!!!!

You just saved me the trouble of having to write the update and delete as
well!

Thanks again!!!!

Quote:
-----Original Message-----



Date: Wednesday, October 02, 2002 11:16 PM
Subject: Re: string.join question


>> I'm accessing MySQL from Python. This is for an insert
>> statement which looks like this:

>Aha... :) Maybe this will help:

>http://cvs.sabren.com/sixthdev/cvsweb.cgi/storage/MySQLStorage.py?rev...

>Cheers,


>------------------------------------------------------------
>Switch to Cornerhost!             http://www.cornerhost.com/
> Low Priced, Reliable Blog Hosting, With a Human Touch. :)
>------------------------------------------------------------



Mon, 21 Mar 2005 12:23:03 GMT  
 string.join question
Thanks. Got enough here to keep me busy for awhile....
Quote:
-----Original Message-----



Date: Wednesday, October 02, 2002 11:19 PM
Subject: RE: string.join question

>> Hmmmm.... I'm going to have to figure this out.
>> I can test for a ' or " and respond with an invalid entry remark (which
it
>> would be...).

>  one_possible_likely_value = """He said, "I don't think you really want to
>omit quotation marks, do you?" """

><wink>

>> I'm ok with everything except the statement:
>> ([quote(v) for v in values])

>Did you notice the function quote() I supplied?

>  new_list = [func(x) for x in seq]

>is called a list comprehension.  It's just a different way to write this:

>  new_list = []
>  for x in seq:
>    new_list.append(func(x))

>Please reply to the list, by the way, since this is all on topic and others
>may have more to contribute.

>Cheers,

>// mark



Mon, 21 Mar 2005 12:56:05 GMT  
 string.join question

October 2002 06:47 pm:

Quote:
> Trying to join a string as follows: string.join(['\,'aaa','\],'')
> (the last characters are 2 single quotes).
> I want a character string which looks like 'aaa' (with quotes).
> Anybody know what I'm doing wrong? or if this is possible with
> string.join? Thanks.

        But what are the \'s supposed to be (or did stuff get lost in your
post).

        Off hand, as it is quoted above, your have an open quote, \, comma,
close quote, aaa (which is not inside a string so what is Python
supposed to be doing), open quote, comma, close quote, \]...

Quote:
>>> print string.join(['','aaa',''],"'")

'aaa'

Quote:
>>> aaa = "A long string of nothing"
>>> print string.join(['',aaa,''],"'")

'A long string of nothing'

--
 > ============================================================== <


 > ============================================================== <
 >        Bestiaria Home Page: http://www.beastie.dm.net/         <
 >            Home Page: http://www.dm.net/~wulfraed/             <



Mon, 21 Mar 2005 10:54:54 GMT  
 string.join question
andy surany fed this fish to the penguins on Wednesday 02 October 2002
08:28 pm:

Quote:
> I'm accessing MySQL from Python. This is for an insert statement which
> looks like this:

>    values = [input1, input2]
> # input values are derived from askstring, ex: input1 = aaa, input
> # =bbb
>    sqlcmd = sql.Insert_Cmd (tables, columns, values)

-----------------------------------------------------------------------
Quote:
>   def Insert_Cmd(self,tables,fields,values):

>     insert_str = 'INSERT INTO %s ( %s ) VALUES ( %s )' %

(string.join(tables,','),string.join(fields,','),string.join(values,','))

Quote:

> This yields: INSERT INTO tablename (column1,column2) VALUES (aaa,bbb)

> What I need is:
> INSERT INTO tablename (column1,column2) VALUES ('aaa','bbb')

>>> def squote(tuple):

...     otuple = [string.join(['',x,''],"'") for x in tuple]
...     return otuple
...
Quote:
>>> a = ('123', 'abc', "cde")
>>> b = squote(a)
>>> print string.join(b,',')

'123','abc','cde'

        OR, in ~one line:

Quote:
>>> insert_str = "INSERT INTO %s ( %s ) VALUES (%s)" % ("atable",

...     string.join(("c1", "c2", "c3"),","),
...     string.join([string.join(['',x,''],"'") for x in
("val1","val2","val3")],','))
Quote:
>>> print insert_str

INSERT INTO atable ( c1,c2,c3 ) VALUES ('val1','val2','val3')

--
 > ============================================================== <


 > ============================================================== <
 >        Bestiaria Home Page: http://www.beastie.dm.net/         <
 >            Home Page: http://www.dm.net/~wulfraed/             <



Mon, 21 Mar 2005 13:16:02 GMT  
 string.join question

Quote:

> Trying to join a string as follows: string.join(['\,'aaa','\],'')
> (the last characters are 2 single quotes).
> I want a character string which looks like 'aaa' (with quotes). Anybody

Have you tried repr('aaa') ... ?  string.join (an old usage) or the
join method of string objects (a newer one) seem to have little to
do with the effect you say you want to achieve.

Alex



Mon, 21 Mar 2005 14:39:02 GMT  
 string.join question
Mongo:

Looks like you have a copy-paste error in your testing of the previous
poster's reply:

He said to do:
a = string.join(string.join(["'", value,"'"],''),'')
                                                  ^
                                              '' not ','

However, you should probably upgrade your python, since that format
has been deprecated, and join is now a method of string objects, not
the string module.
As such, the new way to do this is:

a = ''.join(["'", "aaa", "'"])

I know this suggestion came before, but it got a little garbled from
my POV.

Good luck,

Quote:

> Thanks - it "kind of" works - but not quite. So now there is a part 2 of the
> question:

> value = 'aaa'
> a = string.join(string.join(["'", value,"'"],''),',')

> This should yield (what I want.....): 'aaa',

> Instead it yields: ',a,a,a,'

> ??????????????????????? Looks crazy to me......

> -----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
>    http://www.newsfeed.com       The #1 Newsgroup Service in the World!
> -----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----



Mon, 21 Mar 2005 22:50:14 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Oops: difference in operation of string.join and ''.join

2. string.join() syntax quirky?

3. string.join is abysmally slow

4. bug in string.join()?

5. Joining string to a variable (newbie)

6. String.join revisited (URGENT for 1.6)

7. string.join() vs % and + operators

8. join vs format for giant strings?

9. Need Help with joining strings, ESL interpreter.

10. Need to join two strings?Help

11. Joining strings versus augmented assignment

12. Newbie question: behaviour of String === String

 

 
Powered by phpBB® Forum Software