У меня есть домашнее задание, которое я застрял в течение нескольких дней. Basic Описание проблемы:Использование классов и списков Python для печати отчетов из csv
Incident класс имеет свойства: ID, время, тип, местоположение, описательную и статус
методы: Инициализационные, кратко, isMorning, решить
скрипт принимает один аргумент, то полный путь сообщения о преступлениях csv.
Первые несколько строк CSV:
ID Time Type Location Narrative
1271 11:54 AM Drug Violation Wolf Ridge Report of possible drug violation. Student was referred to the university.
Мой код до сих пор:
import sys
class Incident:
def __init__(self, ID, time, type, location, narrative, status):
self.ID = id
self.time = time
self.type = type
self.location = location
self.narrative = narrative
self.status = status
def brief(self):
print '''{0}: {1}, {2}
{3}
'''.format(self.ID, self.type, self.status, self.narrative)
def isMorning(self):
if 'AM' in self.time:
return True
else:
return False
def resolve(self):
if self.status == 'Pending':
self.status = 'Resolved'
try:
dataset = sys.argv[1] except IndexError:
print 'Usage: Requires full path input file name.'
sys.exit()
# Create an empty list to contain the Incident objects. crimeList = []
# Read the crime report. with open(dataset, 'r') as f:
# Read the header.
headers = f.readline().split(',')
# Read each record and parse the attributes.
for line in f:
lineList = line.strip().split(',')
reportNumber = lineList[0]
timeReported = lineList[1]
incidentType = lineList[2]
location = lineList[3]
narrative = lineList[4]
status = lineList[5].strip()
### Create initialize an Incident object instance and store it in a variable
crime = Incident(reportNumber, timeReported, incidentType, location, narrative, status)
### Append the new Incident object to the crimeList.
crimeList.append(crime)
Что я застрял на: Мне нужно получить доступ к "п-й" Инцидент в crimeList и запускать различные методы. Кажется, я не могу найти способ доступа к элементу и использовать его для запуска методов.
Я пробовал перечислить и сплайсировать, но просто не могу заставить что-нибудь работать?
У кого-нибудь есть предложения?
Почему 'crimeList [п]' не работает? Какая у вас ошибка? – Bahrom
Извините, я новичок здесь, не могу понять, как закодировать комментарии. –
Хорошо, так в чем ваш вопрос? – Bahrom