Python get two values in a Search Cursor than go to next line

4097
12
Jump to solution
06-18-2015 07:22 AM
deleted-user-MS0lE1F_0-sy
Occasional Contributor

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
0 Kudos
1 Solution

Accepted Solutions
TomSellsted
MVP Regular Contributor

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

View solution in original post

12 Replies
IanMurray
Frequent Contributor

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  
TomSellsted
MVP Regular Contributor

Matthew,

Some logic like Ian describes will work.  You can use the modulo operator to accomplish this too.

if (count%2) == 0:

Regards,

Tom

IanMurray
Frequent Contributor

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!).

TomSellsted
MVP Regular Contributor

There are always lots of different ways to solve problems!

0 Kudos
DanPatterson_Retired
MVP Emeritus

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
>>>
0 Kudos
TomSellsted
MVP Regular Contributor

You are correct sir!

0 Kudos
deleted-user-MS0lE1F_0-sy
Occasional Contributor

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
0 Kudos
TomSellsted
MVP Regular Contributor

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

deleted-user-MS0lE1F_0-sy
Occasional Contributor

Perfect! Thank you all, I love this community, I have learned a lot here.  

0 Kudos