[GIS] Creating tables on ArcGIS with arcpy

arcgis-10.2arcpyattribute-tablecursorindexerror

I'm using python scripts on ArcGIS 10.2 to read excel files with information and put this results on attribute tables of the ArcGIS.

I know how to create a table, add rows, add fields and send information to tables.

from arcpy import env
env.workspace = "F:Tr/Model/Results"
output="Tr/Model/Results"
arcpy.CreateTable_management(output, "ger_rea.dbf")

#Create fields
arcpy.env.workspace = "F:Tr\Model\Results"
arcpy.AddField_management("ger_rea.dbf", "Barr", "Text", 50, "", "", "refcode", "NULLABLE", "REQUIRED")
arcpy.AddField_management("ger_rea.dbf", "Ge_1", "Float",  "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management("ger_rea.dbf", "Ge_2", "Float",  "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")

table_ge_rea="F:Tr\Model\Results\ger_rea.dbf" 

#Add rows
rows_pa = arcpy.InsertCursor("F:Tr\Model\Results\ger_rea.dbf")
for x in range(1, (L_Nod_name_LA+1)):
    row = rows_pa.newRow()
    rows_pa.insertRow(row)

#Put information on created table
gr=0
with arcpy.da.UpdateCursor(table_ge_rea,["Barr","OID"]) as cursor:
    for row in cursor:
        row[0]=Nod_name_GA[gr]
        row[1]=Act_L_value[ga]
        cursor.updateRow(row)
        gr=gr+1

However I need to create an automatically tool and The number os rows and fields of the excel tables can change and consequently tables that I'm creating with arcpy need to change too. For example: where I had a table with fields "Barr", "Ge_1", "Ge_2" I can have "Barr", "Ge_1", "Ge_2", "Ge_3", "Ge_4", etc…

How can I create a table (with arcpy) automatically, knowing the number of fields and rows (information on excel that I know how to read), using a for or while loop?

I tryed also use "arcpy excel to table" to export directly a sheet of an excel file to ArcGIS, using this code:

import arcpy
arcpy.env.workspace = "F:\Otim\inter"
arcpy.ExcelToTable_conversion("02_Reactiv.xlsx", "outger.gdb", "PoGenRe")

where 02_Reactiv.xlsx is the excel file, outger.gdb is the name of the output table and PoGenRe is the name of the sheet that I want to pass to ArcGIS.
This sheet contain: on the firts line only two columns filled, and Below, the information that I want to pass: 13 lines, 3 columns, 4 columns
in blank and 2 columns with sum of some values, etc..

It gives me an error:

Runtime error Traceback (most recent call last): File "",
line 1147, in File "c:\program files
(x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 44, in
ExcelToTable raise e ExecuteError: Traceback (most recent call
last): File "c:\program files
(x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py", line
254, in arcpy.GetParameterAsText(2)) File "c:\program
files (x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py",
line 205, in excel_to_table out_fields = gen_out_fields(workbook,
sheet, out_path, is_gdb) File "c:\program files
(x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py", line
177, in gen_out_fields out_fields.append(clsField(f, out_path,
is_gdb, out_fields)) File "c:\program files
(x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py", line 32,
in init if not name[0].isalpha(): IndexError: string index out
of range Failed to execute (ExcelToTable).

Best Answer

Your arcpy.env.workspace = "F:\Otim\inter" slashes are not correct. Use one of these instead:

arcpy.env.workspace = r"F:\Otim\inter"

or

arcpy.env.workspace = "F:\\Otim\\inter"

or

arcpy.env.workspace = "F:/Otim/inter"