2016-09-27 1 views
0

を並べ替え:ユーザーは9月6日、2010年前に開始した行のみを検索は辞書を注文し、私は簡単な模擬試験の質問解決しようとしている

  • :CSVはにファイルを解析

    を。

  • 次に、
  • リターン(開始日によって)昇順に「言葉」欄からコンパイル「隠された」というフレーズを値を注文

csvファイルには、19列と1000行のデータがあります。そのほとんどは無関係です。問題が述べるように、start_date列を昇順で並べ替え、関連する単語を 'words'列から取得することにのみ関心があります。一緒に、言葉は "隠された"フレーズを与えるでしょう。

ソースファイルの日付はUTCの時刻形式であるため、変換する必要がありました。今私は正しい行を選択していると思うところですが、日付をソートする際に問題があります。

は、ここに私のコードです:

import csv 
from collections import OrderedDict 
from datetime import datetime 


with open('TSE_sample_data.csv', 'rb') as csvIn: 
    reader = csv.DictReader(csvIn) 
    for row in reader: 

     #convert from UTC to more standard date format 
     startdt = datetime.fromtimestamp(int(row['start_date'])) 
     new_startdt = datetime.strftime(startdt, '%Y%m%d')   

     # find dates before Sep 6th, 2010 
     if new_startdt < '20100906': 

      # add the values from the 'words' column to a list 
      words = [] 
      words.append(row['words']) 

      # add the dates to a list 
      dates = [] 
      dates.append(new_startdt) 

      # create an ordered dictionary to sort the dates... this is where I'm having issues 
      dict1 = OrderedDict(zip(words, dates)) 
      print dict1 
      #print list(dict1.items())[0][1] 
      #dict2 = sorted([(y,x) for x,y in dict1.items()]) 
      #print dict2 

I print dict1は、私は言葉で1つの注文の辞書を持っていることを期待してると日付が項目として含まれます。代わりに、私が得ているのは、作成された各キーと値のペアごとに複数の順序付けられた辞書です。

+1

ラバーダックデバッグがそれを修正する次回は、単に投稿しないでください。他の誰かがこれを便利に見出すことはまずありません。 – jonrsharpe

+0

はい、もちろんです!それはあなたに特に役立つ超*だったでしょう。私の主張は、それがあなたにとって特に唯一のものだということです。同じ問題を抱えている別の人は、どうすれば同じコードを書いているのでなければ、どのようにして問題を解決するのでしょうか?それは質の高い質問と回答を作成することです。[ツアー]をご覧ください。あなたが努力していることに感謝し、あなたの問題を解決したことをうれしく思っていますが、それはこれを有用にしません。 – jonrsharpe

答えて

0

ここで修正されたバージョンです:

import csv 
from collections import OrderedDict 
from datetime import datetime 


with open('TSE_sample_data.csv', 'rb') as csvIn: 
    reader = csv.DictReader(csvIn) 
    words = [] 
    dates = [] 
    for row in reader: 

     #convert from UTC to more standard date format 
     startdt = datetime.fromtimestamp(int(row['start_date'])) 
     new_startdt = datetime.strftime(startdt, '%Y%m%d')   

     # find dates before Sep 6th, 2010 
     if new_startdt < '20100906': 

      # add the values from the 'words' column to a list 
      words.append(row['words']) 
      # add the dates to a list 
      dates.append(new_startdt) 

    # This is where I was going wrong! Had to move the lines below outside of the for loop 
    # Originally, because I was still inside the for loop, I was creating a new Ordered Dict for each "row in reader" that met my if condition 
    # By doing this outside of the for loop, I'm able to create the ordered dict storing all of the values that have been found in tuples inside the ordered dict 
    # create an ordered dictionary to sort by the dates 
    dict1 = OrderedDict(zip(words, dates)) 
    dict2 = sorted([(y,x) for x,y in dict1.items()]) 

    # print the hidden message 
    for i in dict2: 
     print i[1] 
+0

この答えはまったく役に立たない。問題の内容、変更内容、変更がどのように解決されたかについては説明がありません。繰り返しますが、あなたが些細な誤りを犯したことが分かったとき、それがSOにとって有益なQ&Aではないと思います。 – jonrsharpe

関連する問題