将新列表索引分配给现有列表
Assigning new list indices to existing list
在 python 上很新,我正在努力解决一些问题。我从一个 CSV 文件中分离出一堆数据点,其中一个点是一个日期 (mm/dd/yyyy)。我正在获取那个日期并将其拆分为每个“/”,以便将各个部分分成单独的列表。那就是我遇到麻烦的地方。在我的代码末尾,当我尝试打印列表中超过零的每个索引时,我得到了这些错误。最后,我想对这三个单独的日期对象做的事情是将它们作为子列表添加到我现有的 ptInfo 列表(ptInfo[8]、ptInfo[9]、ptInfo[10])的末尾我该怎么做?
运行时错误(IndexOutOfRangeException):索引超出范围:1
追溯:
第 51 行,在脚本中
运行时错误(IndexOutOfRangeException):索引超出范围:2
追溯:
第 52 行,在脚本中
#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])
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']
>>>
在 python 上很新,我正在努力解决一些问题。我从一个 CSV 文件中分离出一堆数据点,其中一个点是一个日期 (mm/dd/yyyy)。我正在获取那个日期并将其拆分为每个“/”,以便将各个部分分成单独的列表。那就是我遇到麻烦的地方。在我的代码末尾,当我尝试打印列表中超过零的每个索引时,我得到了这些错误。最后,我想对这三个单独的日期对象做的事情是将它们作为子列表添加到我现有的 ptInfo 列表(ptInfo[8]、ptInfo[9]、ptInfo[10])的末尾我该怎么做?
运行时错误(IndexOutOfRangeException):索引超出范围:1 追溯: 第 51 行,在脚本中
运行时错误(IndexOutOfRangeException):索引超出范围:2 追溯: 第 52 行,在脚本中
#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])
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']
>>>