Replace Function Issue

Sorry to be the bearer of bad questions...

I'm working on a project for the company and I am looking to do some string manipulation. Here is the link to the replace documentation: Replace Doc.

I first started using the function when a button would be clicked but I get an error similar to: " 'replace' is not defined.", so I then went to the script console to test the replace function and I keep getting the same error. Below is an image of what I got.

What am I doing wrong?

The replce() you're trying to use is an expression function. In Jython/Python, which is what is used in scripting, use:

x = _str.replace('[', '')
4 Likes

Put those in your bookmarks:

scripting functions

expression functions

3 Likes

Thank you so much! I wish the manual would have mentioned it.

It does...
image

3 Likes

Thank you!

Sorry to bother,

I am trying to use the replace function on a button click.
I've tried

_str = replace(x, "[", "")
and
_str = x.replace( "[", "")

and neither work.

What do you mean, they don't work ?
What do you expect to happen, and what does actually happen ?

also, when posting code, use the code formatting tool:
image

You're using x in a function but you have not defined x. Try this:

_str = "[abcds"
x = _str.replace('[', '')
print x

I am using a button component to run a query when the button is clicked. For now it returns 1 row with characters in the string that I need to get rid of and I am using the replace function. Here are the two ways I've used it in the script action:

	data =system.db.runNamedQuery('Test_Path')
	_str =""

	
	for x in data:			
		_str = replace(x, '[', "")
		_str = x.replace("[" ,"")

I get "replaced is not defined" when I run

_str = replace(x, '[', '')

I get "object has no attribute 'replace'" when I run

		_str = x.replace("[" ,"")

Well then clearly x is not a string here.

But frankly I'm surprised that code doesn't choke on for x in data, I'd expect data to be a dataset, which is not iterable as far as I know...

edit: eh, it's actually a PyDataSet. nevermind.

:man_facepalming:
because it's a dataset........
It's too early man.

If your dataset only has one column, you can use this instead:

for x in data.getColumnAsList(0)
1 Like

Are there actually [ characters in the database value, or did you use a print statement to see what was returned?

Good point, it's worth checking

edit: use this to print your dataset:


def print_ds(ds, show_rownums=False):
	"""
	Print a dataset in a markdown compatible format.

	params:
		ds:				Dataset to print
		show_rownums:	If true, a column is added at index 0 with the row number
	"""
	headers = list(ds.columnNames)

	if ds.rowCount == 0:
		print "| {} |".format(' | '.join(headers))
		return

	columns = [ds.getColumnAsList(i) for i in xrange(ds.columnCount)]
	if show_rownums:
		headers = [""] + headers
		columns = [range(ds.rowCount)] + columns
	widths = [
		max(
			max(len(unicode(value)) for value in col),
			len(header)
		) for header, col in izip(headers, columns)
	]
	
	print '| {} |'.format(' | '.join(h.ljust(w) for h, w in izip(headers, widths)))
	print '|{}|'.format('|'.join('-'*(w+2) for w in widths))
	print '\n'.join('| {} |'.format(' | '.join(unicode(v).ljust(w) for v, w in izip(row, widths))) for row in izip(*columns))

You'll need to import izip from itertools. Or replace izip with a simple zip.

Bonus effect of using this: The output's format is markdown compatible:

name
0 foo
1 bar
2 baz
3 pox
4 wuz

You can be forgiven, as this is a fairly recent change. PyDataset has been revised to implement the Dataset interface, so functions that used to return Dataset can now safely return PyDataset. And that change was made to runNamedQuery.

4 Likes

There are characters in the DB, I was just using a 2 dimensional array and not realizing it as @pascal.fragnoud mentioned, it wasn't a string I was working with.

You guys are awesome, sorry for the bother! :slight_smile:

Thank you for all your help! :slight_smile: