update cursor to update csv data by loop iteration error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • irislarin
    New Member
    • Jul 2010
    • 1

    update cursor to update csv data by loop iteration error

    I was wondering if i could ask for some advise. I wrote a script for my workplace that imports CSV data from a template sheet (with data) to a DB table. I wrote it with a series of loops so that I wouldn't have to hardcode the headers names(43 header name). The template is used so that the headers do match exactly the table in the DB. I keep getting a run time error at the line execution the row.name, where name is a variable that varies in value as the loop iterates thought each header name. When I run it through the degugger the explicit name does not exist. Is there an alternate way of doing this so that I dont have to hardcode this into the code?
    Thank you ,

    Iris




    Code:
    import arcgisscripting
    gp = arcgisscripting.create(9.3)
    gp.toolbox = "management"
    
    
    # Define workspace for infile and data out put
    workspace =gp.GetParameterAsText(0)#define workspace
    inFile ='gp.GetParameterAsText(1)# shape out file
    dataOutPut =gp.GetParameterAsText(2)#in geochem file
    gp.workspace = workspace
    
    # open file and read header line.
    openInFile = open(inFile)
    headerLine = openInFile.readline()
    #splits header line by comma
    valueList = headerLine.split(",")
    
    #iterating through the headerlist and create a header list
    index = 0
    for value in valueList:
    #index header names
    header = valueList[index]
    
    
    #while values in the value list iterate through list 
    while index <= valueList.index(header):
    #read the rest of the data
    cValuesList = openInFile.readlines()
    #for data in the data list 
    for cValue in cValuesList:
    #split the datalist by comma
    gValue = cValue.split(",")
    
    #iteralete throught the datalist 
    headerindex = 0
    #create an insert cursor
    rows = gp.insertCursor(dataOutPut)
    #for each data value in the datalist, create a new row
    for gcvalue in gValue :
    row = rows.NewRow()
    Name = str(valueList[headerindex])
    row.Name = gValue[headerindex] #( line 46, in <module> 
    #RuntimeError: ERROR 99999: Error executing function.
    rows.InsertRow(row)
    
    headerindex += 1
    
    
    #add 1 to the counter 
    index += 1 
    
    # Clean up the cursor
    #del rowInserter
    gp.getmessages()
    Last edited by Niheel; Jul 12 '10, 11:56 PM. Reason: please use code tags to display code
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by irislarin
    I was wondering if i could ask for some advise. I wrote a script for my workplace that imports CSV data from a template sheet (with data) to a DB table. I wrote it with a series of loops so that I wouldn't have to hardcode the headers names(43 header name). The template is used so that the headers do match exactly the table in the DB. I keep getting a run time error at the line execution the row.name, where name is a variable that varies in value as the loop iterates thought each header name. When I run it through the degugger the explicit name does not exist. Is there an alternate way of doing this so that I dont have to hardcode this into the code?
    Thank you ,

    Iris




    Code:
    import arcgisscripting
    gp = arcgisscripting.create(9.3)
    gp.toolbox = "management"
    
    
    # Define workspace for infile and data out put
    workspace =gp.GetParameterAsText(0)#define workspace
    inFile ='gp.GetParameterAsText(1)# shape out file
    dataOutPut =gp.GetParameterAsText(2)#in geochem file
    gp.workspace = workspace
    
    # open file and read header line.
    openInFile = open(inFile)
    headerLine = openInFile.readline()
    #splits header line by comma
    valueList = headerLine.split(",")
    
    #iterating through the headerlist and create a header list
    index = 0
    for value in valueList:
    #index header names
    header = valueList[index]
    
    
    #while values in the value list iterate through list 
    while index <= valueList.index(header):
    #read the rest of the data
    cValuesList = openInFile.readlines()
    #for data in the data list 
    for cValue in cValuesList:
    #split the datalist by comma
    gValue = cValue.split(",")
    
    #iteralete throught the datalist 
    headerindex = 0
    #create an insert cursor
    rows = gp.insertCursor(dataOutPut)
    #for each data value in the datalist, create a new row
    for gcvalue in gValue :
    row = rows.NewRow()
    Name = str(valueList[headerindex])
    row.Name = gValue[headerindex] #( line 46, in <module> 
    #RuntimeError: ERROR 99999: Error executing function.
    rows.InsertRow(row)
    
    headerindex += 1
    
    
    #add 1 to the counter 
    index += 1 
    
    # Clean up the cursor
    #del rowInserter
    gp.getmessages()
    Would you mind reposting your code with the correct indentation? It's difficult to follow as is.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      Add a print statement here and it should help.
      Code:
      #iterating through the headerlist and create a header list
      index = 0
      for value in valueList:
          #index header names
          header = valueList[index]
          print index, header, value
      
          #while values in the value list iterate through list 
          print index, valueList.index(header)
          while index <= valueList.index(header):
              ## wouldn't this always be equal since you just did this
              ## which may mean that you going past the end of valueList
              ## but it is difficult to tell without proper indentations
              ##
              ## use for index, value in enumerate(valueList):
              ##         header = value ## doesn't it ?
      Edit: You want to readlines once. Record[0] is the header. Split it into a list. Then read records[1] till the end. Split each record, and the record list should line up with the header list. i.e.
      Code:
      for index, header in enumerate(header_list):
          print header, this_record_list[index]
      Get a simple print statement coded first to make sure everything lines up and then code the rest of the program. Post back with that code if you want some more help.

      Comment

      Working...