Search code examples
pythonlistgrasshopperrhino3d

Assigning new list indices to existing list


Pretty new at python and I'm struggling with something. I'm breaking out a bunch of data points from a CSV file, one point which is a date (mm/dd/yyyy). I'm taking that date and splitting it at each "/" in order to separate the respective parts into separate lists. That is where I'm having trouble. At the end of my code when I try to print each index of the list beyond zero I get these errors. Ultimately the thing I want to do with these three individual date objects is add them as sub-lists to the end of my existing ptInfo List (ptInfo[8], ptInfo[9], ptInfo[10]) How can I do this?

Runtime error (IndexOutOfRangeException): index out of range: 1 Traceback: line 51, in script

Runtime error (IndexOutOfRangeException): index out of range: 2 Traceback: line 52, in script

#import Points from CSV

import rhinoscriptsyntax as rs
import sys
import datetime

input_file = 'C:\Users\kenma\Dropbox (Personal)\Solo Work\Projects\Sweet Crude\Work\data\prepared_uic_data.csv'

#Init Lists
a = []
apis = []           #0
operators = []      #1
operatorNums = []   #2
wellTypes = []      #3
dates = []          #4
lats= []            #5
longs = []          #6
zoneAreas = []      #7
dateFrag = []
dateM = []          #8
dateD = []          #9
dateY = []          #10

file = open(input_file, 'r')    #open file for reading
lines = file.readlines()        #read lines into variable
file.close()                    #close the file
del lines[0]                    #delete first header line

for line in lines:
    #remove the /n
    line = line.strip()

    # split line by the column
    ptInfo = line.split(',')
    a = ptInfo

    # split line data into individual arrays
    apis.append(ptInfo[0])
    operators.append(ptInfo[1])
    operatorNums.append(ptInfo[2])
    wellTypes.append(ptInfo[3])
    dates.append(ptInfo[4])
    lats.append(ptInfo[5])
    longs.append(ptInfo[6])
    zoneAreas.append(ptInfo[7])

    dateFrag = ptInfo[4].split("/")
    print(dateFrag[0])
    print(dateFrag[1])
    print(dateFrag[2])

Solution

  • how do I then add those three list elements to my ptInfo List?

    >>> pt_info = ['a', 'b', 'c']
    >>> d = '01/02/03'
    >>> d.split('/')
    ['01', '02', '03']
    >>> pt_info.extend(d.split('/'))
    >>> pt_info
    ['a', 'b', 'c', '01', '02', '03']
    >>>