AnsweredAssumed Answered

Create a new table out of listed tables including a section

Question asked by JuttaSchiller on Oct 7, 2014
Latest reply on Oct 8, 2014 by JuttaSchiller

I have around 30 tables like this:





Now I have to get one big table out of them, but only for the first four lines of each table.

First I tried it with this code:


resulttxt = newpath + "\\" + "resultSUM.txt"
f = open (resulttxt, 'w')

f.close ()

f = open (resulttxt, 'a')   
fc_tables = arcpy.ListTables("*sort_sort*")
for tableR in fc_tables:
    fieldsSe= ["Combi","FREQUENCY" ,"SUM_PERCEN" ,"SUM_SUM_ar", "NAME", "CODE"]
    with arcpy.da.SearchCursor(tableR, fieldsSe, """"OID" < 4""") as sCursor:
        for row in sCursor:
            print row [0], row [1],row [2],row [3],row [4],row [5]
            f.write(str(row[0]) + "," + str(row[1])+ "," +str(row[2]) + "," + str(row[3])+ "," +str(row[4])+ "," +str(row[5]) + "\n")



The problem with this code is that the field “Combi” of the “resulttxt” has the type “double” therefore it does not fill the combinations in the right way, because the combinations are originally in a “text “-field (see the first table).

This is the result from the code with open():




Because of this I tried the tool "merge" with the following code:

import arcpy
import os, sys
from arcpy import env

env.overwriteOutput = True
env.workspace = r"D:\Users\julia\erste_aufg\cities_UA"
env.qualifiedFieldNames = False

resulttxt = r"D:\Users\julia\erste_aufg\cities_UA\resultfolder"+  "\\" +"4resultSUM.txt"
fc_tables = arcpy.ListTables("*sort_sort*")
for tableR in fc_tables:
    tableresultName = os.path.splitext (tableR) [0]
    print tableresultName
    arcpy.Merge_management(fc_tables, resulttxt)


The new table looks like this:



This is exactly what I need, but how can I do this for the selection of just the first four lines? Now it gives me all (10) back.  Does someone has an idea?