2016-03-23 15 views
1

私は数日間執着していた宿題を持っています。 基本的な問題の説明:Pythonのクラスとリストを使用してCSVからレポートを印刷する

インシデントクラスはプロパティがあります。ID、時間、種類、場所、物語や状況

方法:のinit、簡単に、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に "n番目" 事件にアクセスする必要が とさまざまな方法を実行します。私はアイテムにアクセスしてメソッドを実行するための機能を持つ方法を見つけることができないようです。

私は列挙とスプライシングを試みましたが、何もできませんでしたか?

誰もが何か提案がありますか?

+1

など、x.brief()x.resolve():?x=crimeList[n]、そのインスタンスのメソッドを呼び出しますかどのようなエラーが出ますか? – Bahrom

+0

申し訳ありません、私はここで新しいです、コメントのコード方法を理解できません。 –

+0

さて、あなたの質問は何ですか? – Bahrom

答えて

0

そうのようなあなたのcrimeListからn番目の犯罪を見上げて:なぜ `crimeList [n]は`働いていないです

関連する問題