<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to convert cursor.search results into variables in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306529#M23811</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am quiet new on programing and I need some help. I want to read the values on a table a then turn them into a variable for future calculations. I have no problem with one result from the cursor.search (code bellow). But in some cases I have multiple results (up to 6) on the cursor.search. So how can I assign each of those 6 results to an independent variable ???&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks in advance for your help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EZ 8-D&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt; 
with arcpy.da.SearchCursor(fc1, ("CountryID", "Total_Impact"), whereclause) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Access and print the row values by index position.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Country name: row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Env Impact: row[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('{0} has an impact in {1}'.format(row[0], row[1]))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = row[1]
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 03 Mar 2014 11:26:59 GMT</pubDate>
    <dc:creator>EdwinZea</dc:creator>
    <dc:date>2014-03-03T11:26:59Z</dc:date>
    <item>
      <title>How to convert cursor.search results into variables</title>
      <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306529#M23811</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am quiet new on programing and I need some help. I want to read the values on a table a then turn them into a variable for future calculations. I have no problem with one result from the cursor.search (code bellow). But in some cases I have multiple results (up to 6) on the cursor.search. So how can I assign each of those 6 results to an independent variable ???&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks in advance for your help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EZ 8-D&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt; 
with arcpy.da.SearchCursor(fc1, ("CountryID", "Total_Impact"), whereclause) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Access and print the row values by index position.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Country name: row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Env Impact: row[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('{0} has an impact in {1}'.format(row[0], row[1]))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = row[1]
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Mar 2014 11:26:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306529#M23811</guid>
      <dc:creator>EdwinZea</dc:creator>
      <dc:date>2014-03-03T11:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert cursor.search results into variables</title>
      <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306530#M23812</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello E.Z.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are plenty of ways to do this in python. You could use an array of arrays, like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;listOfRecords=[]
with arcpy.da.SearchCursor(fc1, ("CountryID", "Total_Impact"), whereclause) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Access and print the row values by index position.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; countryName: row.CountryID
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; envImpact: row.Total_Impact
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('{0} has an impact in {1}'.format(row[0], row[1]))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; listOfRecords.append([countryName,envImpact])

#access data
print "Accessing data later in the script!"
for record in listOfRecords:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print('{0} has an impact in {1}'.format(record[0], record[1]))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, I think a dictionary would work better for you in this case. Something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;countryDict={}
with arcpy.da.SearchCursor(fc1, ("CountryID", "Total_Impact"), whereclause) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Access and print the row values by index position.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; countryName: row.CountryID
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; envImpact: row.Total_Impact
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('{0} has an impact in {1}'.format(row[0], row[1]))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; countryDict[countryName]=envImpact

#access data (all records)
print "Accessing data later in the script!"
for country in sorted(countryDict): #sorted() will output the Country in alpha/numeric order
&amp;nbsp;&amp;nbsp;&amp;nbsp; print('{0} has an impact in {1}'.format(country, countryDict[country]))

#access data (one records)
print "Accessing one record!"
print "Canada has a Total Impact of "+str(countryDict["Canada"]) #replace "Canada" with the CountryID for Canada
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;NOTE: dictionaries do NOT allow duplicate keys. In this case the CountryID &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;must&lt;/SPAN&gt;&lt;SPAN&gt; be a &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;unique&lt;/SPAN&gt;&lt;SPAN&gt; field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know how it goes!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;~Josh&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:40:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306530#M23812</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-11T14:40:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert cursor.search results into variables</title>
      <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306531#M23813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey Johs thanks for the input. I kept on searching and start working with the dic idea as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;still my problem is how to convert the values(when there is more than one) from the dic into variables??&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;so far I am using this code&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

arcpy.env.workspace = "D:\GIS\LCAv2.gdb"
fc1 = "D:\\GIS\\LCAv2.gdb\\houses"

def rows_as_dicts(cursor):
&amp;nbsp;&amp;nbsp;&amp;nbsp; colnames = cursor.fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield dict(zip(colnames, row))

# Create field name with the proper delimiters
#
whereclause = """BlockHouse &amp;gt; 0"""
# Use SearchCursor to access state name and the population count
#
with arcpy.da.SearchCursor(fc1, ("materials", "BlockHouse"), whereclause) as sc:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows_as_dicts(sc):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row['materials']
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row['BlockHouse']
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the output is the following&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Cement mortar, at plant (Plaster)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration:underline;"&gt;1123.2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Reinforcing steel, at plant/RER U&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration:underline;"&gt;128.34&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Lightweight concrete block, expanded clay, at plant/CH U&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration:underline;"&gt;2106.0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;my problem is how to convert the values on "BlockHouse" in to variable like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;X= &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;1123.2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Y = &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;128.34&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Z = &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;2106.0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks again for any pointers&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EZ&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:40:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306531#M23813</guid>
      <dc:creator>EdwinZea</dc:creator>
      <dc:date>2021-12-11T14:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert cursor.search results into variables</title>
      <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306532#M23814</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;So you have a field with multiple values you want to parse into individual values? Could you give us an example of some values that field may contain? Are they space, comma, something separated values? Are they geometry objects? Or something else?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also you are using a rather odd method of accessing your row values. Is there a particular reason that you need to use the field names vs the field index to access the values?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Mar 2014 13:36:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306532#M23814</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2014-03-03T13:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert cursor.search results into variables</title>
      <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306533#M23815</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Dear Mathew&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a table, with several columns and rows. What I want to do is to take the values from selected cells and assign them as a variable so I can do some calculations afterwards.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In most of my cases I only need to look at one row and one column so I can get the value pretty easy.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But I have one case where I need to take the values from several rows on a column. I need to assign each of these (cell) values to a different variable. The values are like the one shown on the previous example.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have them already on a dict, now I need is a way to convert the dict values into variable&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again for the interest&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EZ&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Mar 2014 13:43:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306533#M23815</guid>
      <dc:creator>EdwinZea</dc:creator>
      <dc:date>2014-03-03T13:43:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert cursor.search results into variables</title>
      <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306534#M23816</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can access dict values directly as a variable of sorts using the key value, or you can assign them to independent variables.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var1 = dict["key1"]
var2 = dict["key2"]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:40:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306534#M23816</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T14:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert cursor.search results into variables</title>
      <link>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306535#M23817</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello EZ,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I still feel a dictionary is the best solution. I'm not exact sure why you are defining a function. Perhaps I do not understand exactly what you are trying to do. I would use a dictionary like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

arcpy.env.workspace = "D:\GIS\LCAv2.gdb"
fc1 = "D:\\GIS\\LCAv2.gdb\\houses"

whereclause = """BlockHouse &amp;gt; 0"""

dict={}

with arcpy.da.SearchCursor(fc1, ("materials", "BlockHouse"), whereclause) as sc:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sc:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row['materials']
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row['BlockHouse']
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dict[row.materials]=row.BlockHouse


x=d["material 1"]
y=d["material 2"]
z=d["material 3"]
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;If you really want to assign your dictionary to variables (like a,b,c), you can use the exec function. I would avoid this is possible. I feel it adds a lot of unnessesary complexity. This function executes the string version of code you provide it. You could try something like this by adding it after the code above.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import string
chars=list(string.ascii_lowercase)

for i in sorted(dict):
&amp;nbsp;&amp;nbsp;&amp;nbsp; exec(l.pop(0)+"='"+dict&lt;I&gt;+"'")

print a
print b
print c&lt;/I&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This might suit your need, but you now don't have know good way of telling which variable (a,b,c) corresponds to which id (material). It's also hard to tell which letters were have actually been used. If you think you'll have more than 26 variables, we'll have to modify the script.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:40:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-convert-cursor-search-results-into/m-p/306535#M23817</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-11T14:40:26Z</dc:date>
    </item>
  </channel>
</rss>

