Python str.join() concatenation scrambling

Forum rules
Before you post please read this

Python str.join() concatenation scrambling

Postby entorri on Sun Jul 01, 2012 3:04 am

coding a StreamWriter() in Python like builder = StringIO.StringIO() found this interesting
# FAILS
>>> str.join('as %s','dfg')
'das %sfas %sg'
# WORKS
>>> a = """a %s d %s f""" %('da' , 'da')
>>> a
'a da d da f'
----------------
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Linux foo 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux / 3.5 Bogomips
Last edited by entorri on Sun Jul 08, 2012 11:57 pm, edited 1 time in total.
entorri
Level 1
Level 1
 
Posts: 6
Joined: Thu Jun 14, 2012 11:46 pm

Linux Mint is funded by ads and donations.
 

Re: Python str.join() scrap

Postby xenopeek on Sun Jul 01, 2012 5:53 am

Moved here by moderator

That is not how you are supposed to use str.join(), you have given the iterable to join but not the separator string. http://docs.python.org/library/stdtypes.html#str.join. What are you actually trying to do? This works as expected:
Code: Select all
>>> " da ".join("adf")
'a da d da f'
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14725
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Python str.join() scrap

Postby Jamesc359 on Sun Jul 01, 2012 4:27 pm

You need to remember that Python is heavily object oriented. As such a string is treated as an object and join is one of it's many methods.

As shown in the docs the str part of str.join() is used as a symbolic represention for a string object. Creating a real string object for use in actual code is simple.
Code: Select all
l = ["Hello", "World"] # List Object
s = " " # String Object

print s.join(l)
print "_".join(l)

This should produce;
Hello World
Hello_World


I hope that makes sense. :)
“Gosh, you've... really got some nice toys here.”
User avatar
Jamesc359
Level 3
Level 3
 
Posts: 111
Joined: Sun May 06, 2012 1:42 pm

Re: Python str.join() concatenation scrambling

Postby entorri on Sun Jul 08, 2012 11:55 pm

nope it doesn't

i'm pretty sure it's a memory scrambling bug maybe from python's build
* the data scrambles from the end of the string to be joint to the start of the initial string.

the sentence works for a SQLite3 SELECT * and is not working at any point it scrambles the last characters to the first of the sentence.

i'm not using Python SQLite statement joiner since it makes another whole different kind of string concatenation errors
entorri
Level 1
Level 1
 
Posts: 6
Joined: Thu Jun 14, 2012 11:46 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests