2017-12-22 13 views
0

コンテキストを設定するには、自分の要件に従ってcsvファイルを変換して、変換されたcsv出力ファイルを作成する必要があります。DictReaderオブジェクトを使用したCSVファイルからの辞書

私はこの問題文のために、私はDictReaderをpython csvから使用する必要があることを知りました。変換後、私はcsvファイルに永続化する必要があるnew_pointという別の辞書を作成しようとしました。

問題は、ディクショナリの配列でも辞書でもない辞書の印刷時に、辞書の生成に問題があるようです。

with open(out_file, 'w') as f_out, open(in_file, 'r') as f_in: 
     out_colnames = ['duration', 'month', 'hour', 'day_of_week', 'user_type']   
     trip_writer = csv.DictWriter(f_out, fieldnames = out_colnames) 
     trip_writer.writeheader() 

     trip_reader = csv.DictReader(f_in) 

     # collect data from and process each row 
     for row in trip_reader: 
      # set up a dictionary to hold the values for the cleaned and trimmed 
      # data point 
      new_point = {} 
      ## TODO: use the helper functions to get the cleaned data from ## 
      ## the original data dictionaries.        ## 
      ## Note that the keys for the new_point dictionary should match ## 
      ## the column names set in the DictWriter object above.   ## 
      duration = duration_in_mins(row, city) 
      month,hour,day_of_week = time_of_trip(row, city) 
      user_type = type_of_user(row, city) 

      new_point['duration'] = duration 
      new_point['month'] = month 
      new_point['hour'] = hour 
      new_point['day_of_week'] = day_of_week 
      new_point['user_type'] = user_type 

      pprint(new_point) 

私はtype(new_point)を印刷する場合、私は以下になっていますの、new_point辞書を印刷するとき、私は

Class <Dict> 

以下のような出力を取得しています。

{'day_of_week': 'Thursday', 
'duration': 7.123116666666666, 
'hour': 22, 
'month': 3, 
'user_type': 'Registered'} 
{'day_of_week': 'Thursday', 
'duration': 9.792516666666668, 
'hour': 22, 
'month': 3, 
'user_type': 'Registered'} 
{'day_of_week': 'Thursday', 
'duration': 6.632983333333333, 
'hour': 22, 
'month': 3, 
'user_type': 'Registered'} 
{'day_of_week': 'Thursday', 
'duration': 7.4047, 
'hour': 22, 
'month': 3, 
'user_type': 'Registered'} 
{'day_of_week': 'Thursday', 
'duration': 13.014583333333333, 
'hour': 22, 
'month': 3, 
'user_type': 'Registered'} 
{'day_of_week': 'Thursday', 
'duration': 17.0552, 
'hour': 22, 
'month': 3, 
'user_type': 'Registered'} 
{'day_of_week': 'Thursday', 
'duration': 11.01165, 
'hour': 22, 
'month': 3, 
'user_type': 'Registered'} 
{'day_of_week': 'Thursday', 
'duration': 30.01175, 
'hour': 21, 
'month': 3, 
'user_type': 'Registered'} 

DictReaderオブジェクトから辞書を作成するこの方法に既に従っている人は、この問題を解決するのに役立ちますか?

この質問が非常に素朴な私の心からの謝罪を見つける場合。私はPythonに全く新しいです。私はドキュメントを読んでみましたが、そのコンセプトを理解していませんでした。どこでも詳細な説明は見つけられませんでした。

+0

「辞書を印刷すると、辞書や辞書の配列ではありません」その文を説明できますか? *表示されている出力の辞書のように見えます。 – larsks

+0

@larsks私はそれについてはわかりません:)辞書のように見えますが、forループを使って反復すると、dictの場合と同じように機能しません。 – dataEnthusiast

+0

'pprint'行の直後に、' trip_writer.writerow(new_point) 'という行を挿入するのはどうですか? –

答えて

1

私が見たことから、new_pointはよく見えます。これは、あなたの列ヘッダーと同じキーを持つ辞書です。しかし、出力に書き込む文は表示されません。あなたがそれを作った後、あなたの辞書を書き出すのはどうですか?

 new_point['duration'] = duration 
     new_point['month'] = month 
     new_point['hour'] = hour 
     new_point['day_of_week'] = day_of_week 
     new_point['user_type'] = user_type 
     trip_writer.writerow(new_point) # Write to the file 
関連する問題