Modifying a list of fields with arcpy.da.UpdateCursor - arcpy

I have a list of fields from a dataset which are string type fields.
I need to remove empty spaces from all rows in these fields.
My code is:
import arcpy
dataset = r'Database Connections\xxxx.sde\GISUSA.PET_Wells'
fields = arcpy.ListFields(dataset)
for field in fields:
if field.type =="String":
list_of_fields.append(field.name)
for i in list_of_fields:
with arcpy.da.UpdateCursor(dataset, "{}".format(i)) as cursor:
for row in cursor:
row[0]=row[0].strip()
cursor.updateRow(row)
I get an error:
Runtime error Traceback (most recent call last): File "",
line 4, in AttributeError: 'NoneType' object has no attribute
'strip'
I am guessing the value of i in the line
with arcpy.da.UpdateCursor(dataset, "{}".format(i)) as cursor:
is not in the right format.
I have tested a different formatting, '"{}"'.format(i). It too has an error when put inside UpdateCursor.
This code prints correctly formatted values, with double quoatation marks, which are required for fields inside UpdateCursor
>>> for i in list_of_fields:
... print '"{}"'.format(i)
... "WELL_UWI" "WELL_NAME" "ELEV_TYPE" "CURRENT_STATUS"
"SPUD_DATE" "COMPLETION" "FIELD" "WL_COUNTY" "WL_STATE"
"DRILLING_OPERATOR" "CURRENT_WELL_LEASE_NAME" "REMARK"
for i in list_of_fields:
... with arcpy.da.UpdateCursor(dataset, '"{}"'.format(i)) as cursor:
... for row in cursor:
... row[0]=row[0].strip()
... cursor.updateRow(row)
Runtime error Traceback (most recent call last): File "",
line 10, in RuntimeError: Cannot find field '"WELL_UWI"'
Any thoughts?

The values in the list_of_fields are already strings, the UpdateCursor accepts either a list of fields or a string with the name of the field. There is no need to do any kind of formatting to the values.
just do:
for i in list_of_fields:
with arcpy.da.UpdateCursor(dataset, i) as cursor:
for row in cursor:
row[0]=row[0].strip()
cursor.updateRow(row)
Also are you trying to get rid of all empty spaces or just at the beginning and end of the string? If you want to get rid of all spaces you should use string.replace(' ','')

After some trail and error this worked:
for i in list_of_fields:
with arcpy.da.UpdateCursor(dataset, i) as cursor:
for row in cursor:
row[0]=str(row[0]).strip()
cursor.updateRow(row)

Related

Freemarker based component

I'm trying to create a freemarker based component on OpenText TeamSite/LiveSite 16. I've created a custom freemarker skin:
In the "Appearance" area, I've placed the following code:
<h1>${doc.book.title}</h1>
<h2>${doc.book.chapter[0].title}</h2>
<h2>${doc.book.chapter[1].title}</h2>
<#list doc.book.chapter as ch>
<h2>${ch.title}</h2>
</#list>
<h1>${doc.book[0].title[0]}</h1>`
This template was taken from the official Freemarker documentation here.
"Context XML" area:
<Data>
<External>
<Object Scope="local">com.sayHello.Books</Object>
<Method>books</Method>
</External>
</Data>
This will call the "books" method which will return a XML document:
public Document books(RequestContext context) {
Document doc = Dom4jUtils.newDocument();
Element docElement = DocumentHelper.createElement("book");
docElement.addElement("title").addText("Test Book");
Element chapterElement = DocumentHelper.createElement("chapter");
chapterElement.addElement("title").addText("Chapter 1");
Element anotherChapterElement = DocumentHelper.createElement("chapter");
anotherChapterElement.addElement("title").addText("Chapter 2");
docElement.add(chapterElement);
docElement.add(anotherChapterElement);
doc.add(docElement);
return doc;
}
Whenever I preview the component, I get the following error:
2018-10-10 10:26:32,528 ERROR [freemarker.runtime] Error executing FreeMarker template
FreeMarker template error:
For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), or "template output" , but this has evaluated to a sequence+hash (wrapper
==> doc.book.title [in template "template" at line 2, column 7]
Tip: This XML query result can't be used as string because for that it had to contain exactly 1 XML node, but it contains 0 nodes. That is, the constructing XML query has found no matches.
FTL stack trace ("~" means nesting-related):
- Failed at: ${doc.book.title} [in template "template" at line 2, column 5]
I'm guessing I'm not accessing correctly the template elements inside TeamSite, the Freemarker template works fine outside TeamSite.
Any help is appreciated.

Beautiful Soup get the second attribute from element

I have this code:
all_cells = result.find_all('div')
test = all_cells[1].find('a')
Which normally return this line
<a data-toggle='tooltip' data-placement='bottom' data-html='true' title="<img src='https://l.jpg'>" href='details.php?id=234' title='sometext'><b>anothertext</b></a>
I want to get the content of the second "title" attribute (sometext in my example) but also to consider that sometimes the first "title" attribute is missing. As example when the first attribute is missing this will return the info i need.
testok = test['title']

Geb/Spock: setting (required: false) on page content in a module not working

I have a repeating table structure on a page, which looks like this:
<tr>
<td>some text</td>
<td>some integer</td>
<td>empty</td>
<td>some text</td>
<td>empty</td>
<td>contains an input field for the most part, but sometimes it may be blank/radio button/checkbox/etc.</td>
</tr>
I have created a module in Geb for modelling the repeated content, which looks like this:
class RowModule extends Module {
static content = {
cell { $("td", it) }
description { cell(0).text() }
rubrikNum { cell(1).text().toInteger() }
preDefinedValue { cell(3).text() }
inputField(required: false) { cell(5).$("input") }
}
}
I have verified that I am able to fetch all rows that have input fields with the code above. However, whenever I encounter a non-input field in the last "td" I am getting the following error:
geb.error.RequiredPageContentNotPresent: The required page content 'cell - SimplePageContent (owner: modules.RowModule#6fec88c4, args: [5], value: null)' is not present
Since I had defined that content as not required, I expected to be able to fetch all cells and those that did not contain an input field to be null or "EmptyNavigator". I am using Geb 0.12.2 and Spock 1.0.
What am I missing?
I think your problem is, that you you try to access the 5th element of an array which does not exist! something like a indexOutOfBounds exception... the required:false would only work if the 5. cell would be there but without an input element in it.

Handsontable columns attribute prevents data display

As per the documentation here, am trying to add a dropdown in my table.
The following code works fine without the columns attribute..
function getData(obj){
$('body')
.append('<div id="Hot" class="hot handsontable htColumnHeaders"></div>');
var container = document.getElementById('Hot'),hot;
hot2 = new Handsontable(container, {
data:[{
"_____DELETE_____" :"No"
,"CMPCODE" :"H54"
,"CODE" :"666"
,"IFRS_HIERARCHY" :"Goodwill"
}]
,colHeaders: ["_____DELETE_____","CMPCODE","CODE","IFRS_Hierarchy"]
/* the line below prevents data being displayed */
,columns: [{type: 'dropdown',source: ['No','Yes']},{},{},{}]
});
};
It's not the null values either ({}) as it doesn't work purely for the _____DELETE_____ column.
I must be missing something obvious, but can't see it! I'm using v0.20.1 of the .js / .css files.
EDIT - created a fiddle: http://jsfiddle.net/rawfocus/22ubvxaa/
Cracked it.. Each element needs a reference to the actual column it refers to, eg as follows:
var container = document.getElementById('example1'),hot;
hot2 = new Handsontable(container, {
data:[{
"_____DELETE_____" :"No"
,"CMPCODE" :"H54"
,"CODE" :"666"
,"IFRS_HIERARCHY" :"Goodwill"
}]
,colHeaders: ["_____DELETE_____","CMPCODE","CODE","IFRS_Hierarchy"]
/* the line below is working now :-) */
,columns: [
{data:"_____DELETE_____"
,type:'dropdown',source: ["No",'Yes']}
,{data:"CMPCODE"}
,{data:"CODE"}
,{data:"IFRS_HIERARCHY"}
]
});
Fiddle: http://jsfiddle.net/rawfocus/22ubvxaa/2/

Groovy Gives call() Error on List Reference

I am using groovy's SQL querying functions and MarkupBuilder to write xml files. I have had no trouble doing this in the past, but this time there is a bug that is driving me up a wall. Here is the relevant code:
xml.table() {
column {
dataType('string')
label('Month')
}
def numResults = 6
vals = []
names = []
def i = 0
for(i=0;i<numResults;i++){
resultset.next()
vals += resultset.getObject(2)
names += resultset.getObject(3)
column {
dataType('number')
label(resultset.getObject(3))
}
}
row {
month(resultset.getObject(1))
for(i=0;i<numResults;i++){
names[i](vals[i])
}
}
However when it gets to vals[i] in the second for loop I get the following error:
groovy.lang.MissingMethodException: No signature of method: java.lang.String.cal
l() is applicable for argument types: (java.lang.Long) values: [55]
Possible solutions: wait(long), wait(), any(), wait(long, int), each(groovy.lang
.Closure), any(groovy.lang.Closure)
FYI the '55' that is referenced here is the first value in the second column, added to the list in this line: vals += resultset.getObject(2)
Someone, have any idea what is going on here? I have used list a hundred times just like this with zero trouble. Not to mention that the "names" list functions without a hitch...
Whatever resultset is, resultset.getObject(3) is returning a String which you are adding to your list names and then you are trying to call this String like a function, passing the parameter 42
Not sure what you're trying to do, and without a working example, it's hard to suggest a fix

Resources