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