2017-01-17 7 views
-1

私は2つの場所の間の距離と継続時間を取得しようとしています。 私は約2000種類の組み合わせを持っています(実際にはExcelファイルで10000の組み合わせがあります。毎日のGoogleの制限のため5つのファイルに分割する必要があります)、この作業にPythonを使用しようとしました。google maps距離行列の結果をExcelファイルにインポート

結果を得ることに成功しましたが、結果が名前のExcelファイルに保存するのが面倒で難しいです。

誰でも私にExcelファイルの結果を保存する方法を提案できますか?

ここに私のコードがあり、結果ファイルを添付しました。

#way1 

import googlemaps 

gmaps = googlemaps.Client('--')#directions 

import os 
import csv 

def get_file_path(filename): 

    currentdirpath=os.getcwd() 

    file_path = os.path.join(os.getcwd(), filename) 

    print (file_path) 

    return file_path 

path = "C:\\----\\com(eng)1-1.csv" 

def read_csv(filepath): 

    with open(filepath, 'rU', encoding="utf8") as csvfile: 

     reader = csv.reader(csvfile) 

     for row in reader: 

      my_distance = gmaps.distance_matrix(row[0]+", seoul", row[2], mode="transit") 
      print (my_distance) 

read_csv(path) 

これはコードから取得できました。結果から

enter image description here

、私はExcelファイルに距離と時間を必要とするが、私はExcelファイルにインポートする方法がわかりません。誰も私にこれをするのを手伝ってもらえますか?

答えて

0

안녕하세요ㅋㅋ

ので、画像は正しい 'プリント(my_distance)' から結果を示していますか?
このようなjsonモジュールを使用して結果を簡単に処理できます。 JSONについて

import json 

def read_csv(filepath): 

    with open(filepath, 'rU', encoding="utf8") as csvfile: 
     reader = csv.reader(csvfile) 

     for row in reader: 
      my_distance = gmaps.distance_matrix(row[0]+", seoul", row[2], mode="transit") 
      print (my_distance) 

      result_dict = json.loads(my_distance) 
      distance = result_dict['distance']['value'] # meters 
      duration = result_dict['duration']['value'] # seconds 

      # do something 

read_csv(path) 

詳細情報:https://en.wikipedia.org/wiki/JSON

+0

안녕하세요!! ㅋㅋㅋㅋまず、コメントありがとう!試してみます !! –

関連する問題