Hello,
May be you could be in some help with a script I am trying to do for replacing spaces in a string.
I've tried this:
def space2_(*args):
"""replaces space with _"""
return reduce(args.replace(' ','_'), args)
and the console is returning this error:
AttributeError: 'tuple' object has no attribute 'replace'
With a very limited knowledge of python, I am having pain for finding how to transform (*args) into [*args] or anything that would allow the fonction 'replace' to work.
I need this script for fixing a problem with OSCx, which doesn't allow spaces in sent messages.
Thank in advance for any coment.
Patco.
___________________________________________________________________________ Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international. Téléchargez sur http://fr.messenger.yahoo.com
(moving to off-topic)
Since tuples are static after creation & can't be changed but only reassigned, what you have to do is iterate over the items in the tuple to create a new list:
largs = [] for i in range(len(args)): largs.append(args[i].replace(' ', '_'))
return largs
I don't understand why you're trying to use reduce(). This method is designed to call a function recursively over a set of items or over a range, so that each subsequent iteration is called with the result of the previous iteration.
Hopefully this is closer to what you're looking for.
-david
On 4/2/06, patco megalegoland@yahoo.fr wrote:
Hello,
May be you could be in some help with a script I am trying to do for replacing spaces in a string.
I've tried this:
def space2_(*args): """replaces space with _""" return reduce(args.replace(' ','_'), args)
and the console is returning this error:
AttributeError: 'tuple' object has no attribute 'replace'
With a very limited knowledge of python, I am having pain for finding how to transform (*args) into [*args] or anything that would allow the fonction 'replace' to work.
I need this script for fixing a problem with OSCx, which doesn't allow spaces in sent messages.
Thank in advance for any coment.
Patco.
Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international. Téléchargez sur http://fr.messenger.yahoo.com
PD-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
--- david golightly davigoli@gmail.com a écrit : Hi,
(moving to off-topic)
Why does it move to off-topic? the problem comes from OSCx and could be resolved with tcl scripting as well.
Since tuples are static after creation & can't be changed but only reassigned, what you have to do is iterate over the items in the tuple to create a new list:
largs = [] for i in range(len(args)): largs.append(args[i].replace(' ', '_'))
return largs
This returns this error:
largs.append(args[i].replace(' ', '_'))
AttributeError: 'Symbol' object has no attribute 'replace'
I don't understand why you're trying to use reduce(). This method is designed to call a function recursively over a set of items or over a range, so that each subsequent iteration is called with the result of the previous iteration.
ah, yes I didn't delete 'reduce' that comes from many other tests, I've guessed that this fonction changes nothing in this context.
Hopefully this is closer to what you're looking for.
-david
Unfortunately, I couldn't figure it out, but thanks lot for attention,
On 4/2/06, patco megalegoland@yahoo.fr wrote:
Hello,
May be you could be in some help with a script I am trying to do for replacing spaces in a string.
I've tried this:
def space2_(*args): """replaces space with _""" return reduce(args.replace(' ','_'), args)
and the console is returning this error:
AttributeError: 'tuple' object has no attribute 'replace'
With a very limited knowledge of python, I am having pain for finding how to transform (*args) into [*args] or anything that would allow the fonction 'replace' to work.
I need this script for fixing a problem with OSCx, which doesn't allow spaces in sent messages.
Thank in advance for any coment.
Patco.
___________________________________________________________________________ Nouveau : téléphonez moins cher avec Yahoo! Messenger. Appelez le monde entier à partir de 0,012 Â/minute ! Téléchargez sur http://fr.messenger.yahoo.com
Hallo, patco hat gesagt: // patco wrote:
This returns this error:
largs.append(args[i].replace(' ', '_'))
AttributeError: 'Symbol' object has no attribute 'replace'
Your args-tuple is coming in through a pyext inlet then. It is not made up of Python strings then, but of "Symbol"-objects, which are specific to pyext and they represent the Pd symbol atom in Python. You can convert them to a string using the str() builtin. (They get automatically converted in some other uses.
This again can be done very fast using list comprehension:
args_as_string = [str(x) for x in args]
and you can replace spaces with underscores in this step as well:
args_as_string = [str(x).replace(" ","_") for x in args]
Frank Barknecht _ ______footils.org_ __goto10.org__
Yo, I've finally got the good code to put into a pyext script:
def space2_(*args):
"""replaces space with _"""
args_as_string = [str(x).replace(" ","_") for x in args]
return "_".join(args_as_string)
patco.
--- Frank Barknecht fbar@footils.org a écrit :
Hallo, patco hat gesagt: // patco wrote:
This returns this error:
largs.append(args[i].replace(' ', '_'))
AttributeError: 'Symbol' object has no attribute 'replace'
Your args-tuple is coming in through a pyext inlet then. It is not made up of Python strings then, but of "Symbol"-objects, which are specific to pyext and they represent the Pd symbol atom in Python. You can convert them to a string using the str() builtin. (They get automatically converted in some other uses.
This again can be done very fast using list comprehension:
args_as_string = [str(x) for x in args]
and you can replace spaces with underscores in this step as well:
args_as_string = [str(x).replace(" ","_") for x in args]
___________________________________________________________________________ Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international. Téléchargez sur http://fr.messenger.yahoo.com
Okay, the problem has been resolved with using netsend instead of OSC
:D
patco --- david golightly davigoli@gmail.com a écrit : Hi,
(moving to off-topic)
Why does it move to off-topic? the problem comes from OSCx and could be resolved with tcl scripting as well.
Since tuples are static after creation & can't be changed but only reassigned, what you have to do is iterate over the items in the tuple to create a new list:
largs = [] for i in range(len(args)): largs.append(args[i].replace(' ', '_'))
return largs
This returns this error:
largs.append(args[i].replace(' ', '_'))
AttributeError: 'Symbol' object has no attribute 'replace'
I don't understand why you're trying to use reduce(). This method is designed to call a function recursively over a set of items or over a range, so that each subsequent iteration is called with the result of the previous iteration.
ah, yes I didn't delete 'reduce' that comes from many other tests, I've guessed that this fonction changes nothing in this context.
Hopefully this is closer to what you're looking for.
-david
Unfortunately, I couldn't figure it out, but thanks lot for attention,
On 4/2/06, patco megalegoland@yahoo.fr wrote:
Hello,
May be you could be in some help with a script I am trying to do for replacing spaces in a string.
I've tried this:
def space2_(*args): """replaces space with _""" return reduce(args.replace(' ','_'), args)
and the console is returning this error:
AttributeError: 'tuple' object has no attribute 'replace'
With a very limited knowledge of python, I am having pain for finding how to transform (*args) into [*args] or anything that would allow the fonction 'replace' to work.
I need this script for fixing a problem with OSCx, which doesn't allow spaces in sent messages.
Thank in advance for any coment.
Patco.
___________________________________________________________________________ Nouveau : téléphonez moins cher avec Yahoo! Messenger. Appelez le monde entier à partir de 0,012 Â/minute ! Téléchargez sur http://fr.messenger.yahoo.com
At 21:29 02.04.2006, patco wrote:
Hello,
May be you could be in some help with a script I am trying to do for replacing spaces in a string.
I've tried this:
def space2_(*args): """replaces space with _""" return reduce(args.replace(' ','_'), args)
and the console is returning this error:
AttributeError: 'tuple' object has no attribute 'replace'
With a very limited knowledge of python, I am having pain for finding how to transform (*args) into [*args] or anything that would allow the fonction 'replace' to work.
replace only works for strings. there's no replace for lists and tuples. if your *args tuple contains just strings you could convert them all like that:
args = list(args) for i in range(len(args)): args[i] = args[i].replace(' ', '_') return args
forget "reduce", it will be gone in py3 anyway.
sven.
Hallo, patco hat gesagt: // patco wrote:
May be you could be in some help with a script I am trying to do for replacing spaces in a string.
I've tried this:
def space2_(*args): """replaces space with _"""
return reduce(args.replace(' ','_'), args)
You could do:
args = [x.replace(" ","_") for x in args]
This is called "list comprehension" and is very useful.
But if you want to join the elements making up args instead, for example if args is a list coming in through an inlet in [pyext] , you should use join like:
"_".join(args)
Here's the difference of both:
args = ("a", "b", "c d") [x.replace(" ", "_") for x in args]
['a', 'b', 'c_d']
"_".join(args)
'a_b_c d'
Frank Barknecht _ ______footils.org_ __goto10.org__