<?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: ArcPy Table to Table Conversion Not Working Correctly in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-table-to-table-conversion-not-working/m-p/256511#M19746</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;ArcMap/ArcCatalog will automatically create a schema.ini. Please look &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//005s00000010000000" title="http://resources.arcgis.com/en/help/main/10.1/index.html#//005s00000010000000"&gt;Adding an ASCII or text file table&lt;/A&gt;&lt;/P&gt;&lt;P&gt;In absence of schema.ini &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;arcpy.TableToTable_conversion tool might give unexpected results especially when are executing a standalon python script. You can provide the field_mapping parameter or consider creating your own schema.ini file.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;H1 style="margin-bottom: 0.2em; font-size: 22px; font-weight: normal; font-family: 'Avenir LT W01 55 Roman', Arial, Helvetica, sans-serif; color: #000000;"&gt;&lt;/H1&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Jan 2015 19:06:08 GMT</pubDate>
    <dc:creator>MahtabAlam1</dc:creator>
    <dc:date>2015-01-29T19:06:08Z</dc:date>
    <item>
      <title>ArcPy Table to Table Conversion Not Working Correctly</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-table-to-table-conversion-not-working/m-p/256509#M19744</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a script which writes the lat/lng of a json web service of bus routes.&amp;nbsp; The script writes the latitude and longitude to a CSV correctly.&amp;nbsp; However my table to table conversion does not work correctly in Python.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #808080;"&gt;# IMPORTS
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#Make Python understand how to read things on the Internet
&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;import &lt;/SPAN&gt;urllib2

&lt;SPAN style="color: #808080;"&gt;#Make Python understand the stuff in a page on the Internet is JSON
&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;import &lt;/SPAN&gt;json
&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;from &lt;/SPAN&gt;decimal &lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;import &lt;/SPAN&gt;Decimal

&lt;SPAN style="color: #808080;"&gt;# Make Python understand csvs
&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;import &lt;/SPAN&gt;csv

&lt;SPAN style="color: #808080;"&gt;# Make Python know how to take a break so we don't hammer API and exceed rate limit
&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;from &lt;/SPAN&gt;time &lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;import &lt;/SPAN&gt;sleep

&lt;SPAN style="color: #808080;"&gt;# tell computer where to put CSV
&lt;/SPAN&gt;outfile_path=&lt;SPAN style="color: #a5c261;"&gt;'C:\Users\Administrator\PycharmProjects\untitled\json2fgdb.csv'
&lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;# open it up, the w means we will write to it
&lt;/SPAN&gt;writer = csv.writer(&lt;SPAN style="color: #8888c6;"&gt;open&lt;/SPAN&gt;(outfile_path&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;'wb'&lt;/SPAN&gt;))

&lt;SPAN style="color: #808080;"&gt;#create a list with headings for our columns
&lt;/SPAN&gt;headers = [&lt;SPAN style="color: #a5c261;"&gt;'latitude'&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;'longitude'&lt;/SPAN&gt;]

&lt;SPAN style="color: #808080;"&gt;#write the row of headings to our CSV file
&lt;/SPAN&gt;writer.writerow(headers)


&lt;SPAN style="color: #808080;"&gt;# GET JSON AND PARSE IT INTO DICTIONARY
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;# We need a loop because we have to do this for every JSON file we grab
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#set a counter telling us how many times we've gone through the loop, this is the first time, so we'll set it at 1
&lt;/SPAN&gt;i=&lt;SPAN style="color: #6897bb;"&gt;1
&lt;/SPAN&gt;&lt;SPAN style="color: #6897bb;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#loop through pages of JSON returned, 100 is an arbitrary number
&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;while &lt;/SPAN&gt;i&amp;lt;&lt;SPAN style="color: #6897bb;"&gt;2&lt;/SPAN&gt;:
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#print out what number loop we are on, which will make it easier to track down problems when they appear
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;print &lt;/SPAN&gt;i
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#create the URL of the JSON file we want. We search for 'egypt', want English tweets,
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#and set the number of tweets per JSON file to the max of 100, so we have to do as little looping as possible
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;url = urllib2.Request(&lt;SPAN style="color: #a5c261;"&gt;&lt;SPAN&gt;'&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://api.metro.net/agencies/lametro/routes/704/vehicles/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://api.metro.net/agencies/lametro/routes/704/vehicles/&lt;/A&gt;&lt;SPAN&gt;' &lt;/SPAN&gt;&lt;/SPAN&gt;+ &lt;SPAN style="color: #8888c6;"&gt;str&lt;/SPAN&gt;(i))
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#use the JSON library to turn this file into a Pythonic data structure
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;parsed_json = json.load(urllib2.urlopen(&lt;SPAN style="color: #a5c261;"&gt;&lt;SPAN&gt;'&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://api.metro.net/agencies/lametro/routes/704/vehicles/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://api.metro.net/agencies/lametro/routes/704/vehicles/&lt;/A&gt;&lt;SPAN&gt;'&lt;/SPAN&gt;&lt;/SPAN&gt;))
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#now you have a giant dictionary.
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#Type in parsed_json here to get a better look at this.
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#You'll see the bulk of the content is contained inside the value that goes with the key, or label "results".
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#Refer to results as an index. Just like list[1] refers to the second item in a list,
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#dict['results'] refers to values associated with the key 'results'.
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;print &lt;/SPAN&gt;parsed_json



&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#run through each item in results, and jump to an item in that dictionary, ex: the text of the tweet
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;for &lt;/SPAN&gt;items &lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;in &lt;/SPAN&gt;parsed_json[&lt;SPAN style="color: #a5c261;"&gt;'items'&lt;/SPAN&gt;]:

&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#initialize the row
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;row = []
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#add every 'cell' to the row list, identifying the item just like an index in a list
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;row.append(&lt;SPAN style="color: #8888c6;"&gt;str&lt;/SPAN&gt;(items[&lt;SPAN style="color: #a5c261;"&gt;'longitude'&lt;/SPAN&gt;]).encode(&lt;SPAN style="color: #a5c261;"&gt;'utf-8'&lt;/SPAN&gt;))
&amp;nbsp; row.append(&lt;SPAN style="color: #8888c6;"&gt;str&lt;/SPAN&gt;(items[&lt;SPAN style="color: #a5c261;"&gt;'latitude'&lt;/SPAN&gt;]).encode(&lt;SPAN style="color: #a5c261;"&gt;'utf-8'&lt;/SPAN&gt;))

&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#once you have all the cells in there, write the row to your csv
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;writer.writerow(row)
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080;"&gt;#increment our loop counter, now we're on the next time through the loop
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;i = i +&lt;SPAN style="color: #6897bb;"&gt;1
&lt;/SPAN&gt;&lt;SPAN style="color: #6897bb;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;#tell Python to rest for 5 secs, so we don't exceed our rate limit
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp; #sleep(5)
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #cc7832; font-weight: bold;"&gt;import &lt;/SPAN&gt;arcpy
&amp;nbsp; arcpy.TableToTable_conversion(outfile_path&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;"C:\dev_folder\orginalDev.gdb"&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;"jsoncsv2"&lt;/SPAN&gt;



)&lt;/PRE&gt;&lt;P&gt;&lt;IMG alt="previewCatalog.PNG" class="jive-image image-1" src="https://community.esri.com/legacyfs/online/55729_previewCatalog.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;EM&gt;This is my table output when running in Python.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;IMG alt="previewTableTool.PNG" class="jive-image image-2" src="https://community.esri.com/legacyfs/online/55859_previewTableTool.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;EM&gt; This is the output, when using ArcToolBox in ArcCatalog.&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:39:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-table-to-table-conversion-not-working/m-p/256509#M19744</guid>
      <dc:creator>GeoffreyWest</dc:creator>
      <dc:date>2021-12-11T12:39:15Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy Table to Table Conversion Not Working Correctly</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-table-to-table-conversion-not-working/m-p/256510#M19745</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would probably create the output table first, add the fields and use an insert cursor to fill the table:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
fgdb = r"C:\dev_folder\orginalDev.gdb"
tbl_name = "jsoncsv2"
tbl = os.path.join(fgdb, tbl_name)
fld_lat = "Latitude"
fld_lon = "Longitude"

arcpy.CreateTable_management(fgdb, tbl_name)
arcpy.AddField_management(tbl, fld_lon, "DOUBLE")
arcpy.AddField_management(tbl, fld_lat, "DOUBLE")

flds = (fld_lon, fld_lat)
with arcpy.da.InsertCursor(tbl, flds) as curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for items in parsed_json['items']:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = (float(items['longitude']), float(items['latitude']), )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs.insertRow(row)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:39:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-table-to-table-conversion-not-working/m-p/256510#M19745</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T12:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy Table to Table Conversion Not Working Correctly</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-table-to-table-conversion-not-working/m-p/256511#M19746</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;ArcMap/ArcCatalog will automatically create a schema.ini. Please look &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//005s00000010000000" title="http://resources.arcgis.com/en/help/main/10.1/index.html#//005s00000010000000"&gt;Adding an ASCII or text file table&lt;/A&gt;&lt;/P&gt;&lt;P&gt;In absence of schema.ini &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;arcpy.TableToTable_conversion tool might give unexpected results especially when are executing a standalon python script. You can provide the field_mapping parameter or consider creating your own schema.ini file.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;H1 style="margin-bottom: 0.2em; font-size: 22px; font-weight: normal; font-family: 'Avenir LT W01 55 Roman', Arial, Helvetica, sans-serif; color: #000000;"&gt;&lt;/H1&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Jan 2015 19:06:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-table-to-table-conversion-not-working/m-p/256511#M19746</guid>
      <dc:creator>MahtabAlam1</dc:creator>
      <dc:date>2015-01-29T19:06:08Z</dc:date>
    </item>
  </channel>
</rss>

