# Replace Function Issue

**URL:** <https://forum.inductiveautomation.com/t/replace-function-issue/85685>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [February 14, 2024, 2:17pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685 "2024-02-14T14:17:07Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 14, 2024, 2:17pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/1 "2024-02-14T14:17:07Z")

</div>

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](https://docs.inductiveautomation.com/display/DOC80/replace).

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?

 ![Capture](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/b/1b69ba28d4127faafcf8f99d027fce5edb9cd6bc.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 14, 2024, 2:20pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/2 "2024-02-14T14:20:17Z")

</div>

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

```auto
x = _str.replace('[', '')

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 14, 2024, 2:22pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/3 "2024-02-14T14:22:33Z")

</div>

Put those in your bookmarks:

[scripting functions](https://docs.inductiveautomation.com/display/DOC81/System+Functions)

[expression functions](https://docs.inductiveautomation.com/display/DOC81/Expression+Functions)

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 14, 2024, 2:23pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/4 "2024-02-14T14:23:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 14, 2024, 2:23pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/5 "2024-02-14T14:23:40Z")

</div>

It does...  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/9/c972eeecefca8b7d59ac8f6525556a94e9b5762d.png)

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 14, 2024, 2:24pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/6 "2024-02-14T14:24:19Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 14, 2024, 3:03pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/7 "2024-02-14T15:03:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 14, 2024, 3:09pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/8 "2024-02-14T15:09:25Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/b/eb42f78fd88dd3860c546f8d7f22bfe2adb69aa7.png)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 14, 2024, 3:09pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/9 "2024-02-14T15:09:48Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 14, 2024, 3:20pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/10 "2024-02-14T15:20:17Z")

</div>

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:

```auto
	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

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 14, 2024, 3:23pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/11 "2024-02-14T15:23:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 14, 2024, 3:24pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/12 "2024-02-14T15:24:33Z")

</div>

🤦‍♂️  
because it's a dataset........  
It's too early man.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 14, 2024, 3:28pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/13 "2024-02-14T15:28:03Z")

</div>

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

```python
for x in data.getColumnAsList(0)

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 14, 2024, 3:28pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/14 "2024-02-14T15:28:50Z")

</div>

> [@Miguel\_Villasenor\_Es](#):
>
> row with characters in the string

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

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 14, 2024, 3:30pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/15 "2024-02-14T15:30:17Z")

</div>

Good point, it's worth checking

edit: use this to print your dataset:

```python

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 |

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 14, 2024, 4:03pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/16 "2024-02-14T16:03:42Z")

</div>

> [@pascal.fragnoud](#):
>
> it's actually a `PyDataSet`. nevermind.

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`.

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 14, 2024, 4:54pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/17 "2024-02-14T16:54:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 15, 2024, 12:46pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/18 "2024-02-15T12:46:17Z")

</div>

You guys are awesome, sorry for the bother! 🙂

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [February 15, 2024, 12:47pm UTC](https://forum.inductiveautomation.com/t/replace-function-issue/85685/19 "2024-02-15T12:47:02Z")

</div>

Thank you for all your help! 🙂
