I am trying to write my getValues to a text element loop, but I want there to be two results per line before going to the new line. I am having trouble figuring it out? I think I might new a while loop perhaps instead of a for loop, and probably need to use next() somewhere? But that seems to just loop through each name and put one result instead of all four.
I don't want this: Anna I want this: Anna Ben
Ben Charlie Dora
Charlie
Dora
I don't know what to use in here to achive this.
rows = arcpy.SearchCursor(Layer) textValue = "" for row in rows: textValue += row.getValue(Value1) + '\r\n' ParcelText2.text = textValue del rows
Solved! Go to Solution.
Matthew,
Just a bit of a logic error. This should work:
rows = arcpy.SearchCursor(Layer) textValue = "" count = 1 for row in rows: if (count%2): textValue += row.getValue(Value1) + ' ' else: textValue += row.getValue(Value) + '\r\n' count += 1 ParcelText2.text = textValue
Regards,
Tom
should be fairly easy to use a counter or something similar to check.
rows = arcpy.SearchCursor(Layer) textValue = "" for row in rows: count = 1 if count/2.0.is_integer() == False: textValue += row.getValue(Value1) + ' ' count += 1 else: textValue += row.getValue(Value1) + '\r\n' count += 1 ParcelText2.text = textValue del rows
Matthew,
Some logic like Ian describes will work. You can use the modulo operator to accomplish this too.
if (count%2) == 0:
Regards,
Tom
That was probably the better one to use, my general python gets rustier and rustier I feel like since I am only use python within the arcpy module these days(and with a limited usage at that!).
There are always lots of different ways to solve problems!
in fact you don't even need the equality test since it will either be 1 or 0
which equates to a Boolean without the danger of comparing to a number
>>> a = [ not(i%2) for i in range(11)] >>> b = [ (i%2)== 0 for i in range(11)] >>> a [True, False, True, False, True, False, True, False, True, False, True] >>> b [True, False, True, False, True, False, True, False, True, False, True] >>> a == b True >>>
You are correct sir!
Ian/Tom,
I have tried the different versions and it still gets written like this: Anna & I want this: Anna Ben
Ben Charlie Dora
Charlie
Dora
rows = arcpy.SearchCursor(Layer) textValue = "" for row in rows: count = 1 if (count%2) == 0: textValue += row.getValue(Value1) + ' ' count += 1 else: textValue += row.getValue(Value) + '\r\n' count += 1 ParcelText2.text = textValue
Matthew,
Just a bit of a logic error. This should work:
rows = arcpy.SearchCursor(Layer) textValue = "" count = 1 for row in rows: if (count%2): textValue += row.getValue(Value1) + ' ' else: textValue += row.getValue(Value) + '\r\n' count += 1 ParcelText2.text = textValue
Regards,
Tom
Perfect! Thank you all, I love this community, I have learned a lot here.