<?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 Re: Extract specific strings (more than one) into new fields in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91222#M7113</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If it were me I'd focus on getting the data squared away first instead of attempting to cobble it all together with scripting.&amp;nbsp; Use some proper database design technique and get the data of interest into appropriate fields --- you may even want to use another tool that has friendlier string manipulation functions like MS Excel, THEN get your spatial data in order.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wish I had a direct solution for you!&amp;nbsp; But in your case it appears that you will be attempting to apply database methodology (what GIS data really is) to a hodgepoge of strings contained in a single field (an improper database design approach).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 10 Jul 2013 11:19:13 GMT</pubDate>
    <dc:creator>JamesCrandall</dc:creator>
    <dc:date>2013-07-10T11:19:13Z</dc:date>
    <item>
      <title>Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91218#M7109</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 want to extract strings from a field called "other_tags" using the field calculator. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"other_tags" has a lot of unused information and the sting part that I want to extract is always located at a different position. Here some examples of the field "other_tags":&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"lit"=&amp;gt;"yes","maxspeed"=&amp;gt;"50","oneway"=&amp;gt;"yes","ref"=&amp;gt;"B 6","voltage"=&amp;gt;"110000"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"voltage"=&amp;gt;"110000"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"lit"=&amp;gt;"yes","voltage"=&amp;gt;"110000"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"lanes"=&amp;gt;"2","oneway"=&amp;gt;"yes","ref"=&amp;gt;"B 6","voltage"=&amp;gt;"110000"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Always when the string " "voltage"=&amp;gt;" " accures the subsequent number should be written in a new field. Lets call the new field "Result1". It is also possible that the stirng " "voltage"=&amp;gt;" " existes 2,3 or 4 times in a single string so somehow i would have to write the second/third and fourth value in other fields. Lets call them "Result2", "Result3 and "Result4".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There was a similar thread wich didnt really help me further because my data in the field is not structured that way. But maybe it representes some productive food for thought:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/45108-Extract-string-in-field-calculator-using-python"&gt;http://forums.arcgis.com/threads/45108-Extract-string-in-field-calculator-using-python&lt;/A&gt;&lt;BR /&gt;&lt;SPAN&gt;Also I have the same problem then the auditor of that thread that the code he was using does not work with field calculator!? Even when my source field is a sting type just as the target field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From my point of view it seams to be a bretty complex topic for someone like me whose python experience is very small - So I am glad for any kind of help!!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Jul 2013 09:41:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91218#M7109</guid>
      <dc:creator>AlexanderPichlmayr</dc:creator>
      <dc:date>2013-07-09T09:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91219#M7110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Did you try something like this?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;txtStr = '"lit"=&amp;gt;"yes","maxspeed"=&amp;gt;"50","voltage"=&amp;gt;"900","voltage"=&amp;gt;"666","oneway"=&amp;gt;"yes","ref"=&amp;gt;"B 6","voltage"=&amp;gt;"110000"'
sList = txtStr.split(',')
newList = []
for q in sList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if q.find("voltage") &amp;gt;= 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newList.append(q)
for n in newList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print n&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This yeilds a list of just the ","voltage"=&amp;gt;... components of the value string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Not a complete solution, but perhaps a start.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:29:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91219#M7110</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2021-12-10T23:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91220#M7111</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi thanks for your reply,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;well as I said my python skills are still very lame so there are some things a little unclear to me:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) why did you add more then one voltage value in txtStr? The list I am extracting from is very long so there is no way to know what values for voltage exist. Also the shape of all the unused information is unknown - I suppose all existing shapes have to be included in txtStr in order to get it to work!? &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;txtStr = '"lit"=&amp;gt;"yes","maxspeed"=&amp;gt;"50","voltage"=&amp;gt;"900","voltage"=&amp;gt;"666","oneway"=&amp;gt;"yes","ref"=&amp;gt;"B 6","voltage"=&amp;gt;"110000"'&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) what function has &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;newList = []&lt;/PRE&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;for q in sList:&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3) isnt there a pre-logic script code to be set up?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jul 2013 07:01:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91220#M7111</guid>
      <dc:creator>AlexanderPichlmayr</dc:creator>
      <dc:date>2013-07-10T07:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91221#M7112</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, the pre-logic code block will be based on the snippit provided.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;in the code snippit:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;txtStr is the string in the attibute field, and is accessed itteritivly for each record in the table&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;sList = txtStr.split(',')&amp;nbsp; ## this splits the string on the comma delimiters, into a list &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(one entry per keyword_flag=&amp;gt;value)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;newList = []&amp;nbsp; ##&amp;nbsp; this creates a new, empty list. it will hold all the voltage values, however many there may be in that line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(My initial understanding of your remark:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;" It is also possible that the stirng " "voltage"=&amp;gt;" " existes 2,3 or 4 times in a single string " &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;to mean just that: the sample txtStr I used has 3 different voltage flags and values)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The next bit:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for q in sList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if q.find("voltage") &amp;gt;= 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newList.append(q)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;checks each item in the list (the one of the split-up text string)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and, if it finds the string 'voltage', writes it to the new list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now you have a list of just the voltage flags and values:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for example: ["voltage"=&amp;gt;"900","voltage"=&amp;gt;"666","voltage"=&amp;gt;"110000"]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The last bit would not go in the code block: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for n in newList: print n&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;just prints each "voltage"=&amp;gt;"whatever" string to the screen.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It is included here so the snippit does something as a snippit.....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;what you would have to do instead is to set each item in the newList to a variable&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;that is then written to the new attribute item by the Calculator&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You may want to strip off the "voltage"=&amp;gt; part using string.replace()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and perhaps convert the remaining numeric string to actual numbers &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;using short(), long(), or float()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Of course, the Calculator only writes one field, so you will have to get around that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Using an Update Cursor in a custom script is recomended,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;but to use the calculator you could make the calculation in stages:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;you could calculate each Result field (Result1, Result2, Resultx)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;in turn by indexing the newList; as in using newList[0] to calculate Result1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and so on.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:29:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91221#M7112</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2021-12-10T23:29:46Z</dc:date>
    </item>
    <item>
      <title>Re: Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91222#M7113</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If it were me I'd focus on getting the data squared away first instead of attempting to cobble it all together with scripting.&amp;nbsp; Use some proper database design technique and get the data of interest into appropriate fields --- you may even want to use another tool that has friendlier string manipulation functions like MS Excel, THEN get your spatial data in order.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wish I had a direct solution for you!&amp;nbsp; But in your case it appears that you will be attempting to apply database methodology (what GIS data really is) to a hodgepoge of strings contained in a single field (an improper database design approach).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jul 2013 11:19:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91222#M7113</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2013-07-10T11:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91223#M7114</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Excel is a good tool for cobbling...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But getting the data squared away is not so very difficult with scripting.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mucking about with Calculate Field is not the way to do it, however.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Assuming you have already added 6 new Results (numeric) fields to your table&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(Result1, Result2, and so on)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and that the data string is in a field called BigFunkyField&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and the fc is c:\FileGeodatabase.gdb\inFC.....&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

inTable = r"c:\FileGeodatabase.gdb\inFC"
cur = arcpy.UpdateCursor(inTable)
for row in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; txtStr = row.BigFunkyField
&amp;nbsp;&amp;nbsp;&amp;nbsp; sList = txtStr.split(',')
&amp;nbsp;&amp;nbsp;&amp;nbsp; newList = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for q in sList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if q.find("voltage") &amp;gt;= 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; volt = q.replace('"voltage"=&amp;gt;', '')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newList.append(long(volt.replace('"', '')))

&amp;nbsp;&amp;nbsp;&amp;nbsp; length = len(newList)
&amp;nbsp;&amp;nbsp;&amp;nbsp; r1, r2, r3, r4, r5, r6 = 0, 0, 0, 0, 0, 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; rList = [r1, r2, r3, r4, r5, r6]

&amp;nbsp;&amp;nbsp;&amp;nbsp; for n in range(0, length):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rList&lt;N&gt; = newList&lt;N&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(Result1, rList[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(Result2, rList[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(Result3, rList[2])
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(Result4, rList[3])
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(Result5, rList[4])
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(Result6, rList[5])
&amp;nbsp;&amp;nbsp;&amp;nbsp; cur.updateRow(upRow)
del cur

print 'Done'&lt;/N&gt;&lt;/N&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is not elegent, and not tested, but....&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It also turns the number strings into real numbers...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:29:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91223#M7114</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2021-12-10T23:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91224#M7115</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Excel is a good tool for cobbling...&lt;BR /&gt;But getting the data squared away is not so very difficult with scripting.&lt;BR /&gt;Mucking about with Calculate Field is not the way to do it, however.&lt;BR /&gt;&lt;BR /&gt;Assuming you have already added 6 new Results (numeric) fields to your table&lt;BR /&gt;(Result1, Result2, and so on)&lt;BR /&gt;and that the data string is in a field called BigFunkyField&lt;BR /&gt;and the fc is c:\FileGeodatabase.gdb\inFC.....&lt;BR /&gt;&lt;BR /&gt;This is not elegent, and not tested, but....&lt;BR /&gt;It also turns the number strings into real numbers...&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;No doubt.&amp;nbsp; I tend to always approach my development from a database-centric position --- that is, if the database (or GIS layer in this case) is properly designed and maintained, then there is never any need to cobble things together and I set myself up to better be able to develop elegant solutions.&amp;nbsp; Though I'd question if any of my python apps could be considered elegant &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...I am much more of a RDBMS database/N-Tier application developer that is now working in a python scripting world but continually find similarities where I can bring those experiences into my new world.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jul 2013 18:08:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91224#M7115</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2013-07-10T18:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: Extract specific strings (more than one) into new fields</title>
      <link>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91225#M7116</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You will get no disageement from me that &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"if the database (or GIS layer in this case) is properly designed and maintained, then there is never any need to cobble things together".&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Jul 2013 10:41:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extract-specific-strings-more-than-one-into-new/m-p/91225#M7116</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2013-07-11T10:41:27Z</dc:date>
    </item>
  </channel>
</rss>

