2017-09-26 3 views
-1

우선 파이썬에서 completly new라고 말하면서 프로그래밍에 대한 경험이 거의 없습니다. 필자는 직장에서 작업 자동화를위한 환경으로 Python을 사용하기로 결정했습니다. XLS로 내 보낸 날짜를 정렬하고 지오 코드 주소를 지정한 다음 Google API 맵 또는 OpenSourceMaps에서 히트 맵/채우기 맵을 만들어야합니다. SOme 솔루션 나는 이미 발견했지만 문제가 결합되어 매우 어리석은 단계에 접어 들었다.파이썬 3.6 출력 CVS 파일이 비어 있습니다.

참조하십시오 내 코드 :

import os 
import pandas 
import geopy 
import urllib 
import csv 
import numpy 
import geopy 

# - - - - - - - - - 

fin = open ("source_file.csv","r") # open input file for reading 
users_dict = {} 
with open('output_file.csv', 'wb') as fout: # output csv file 
    writer = csv.writer(fout) 
    with open('source_file.csv','r') as csvfile: # input csv file 
     reader = csv.DictReader(csvfile, delimiter=',') 
     for row in reader: 
      location_string = row['city'] + ',' + row['street'] + ' ' + row['house'] 
      from geopy.geocoders import Nominatim 
      geolocator = Nominatim() 
      location = geolocator.geocode(location_string) 
      row['lat']=latitude = location.latitude 
      row['lng'] = location.longitude 
      users_dict.update(row) 
      print (users_dict) 
     fout.close() 
fin.close() 

입력 파일 형식

사전 출력
unit_no,kod,street,house,city,lng,lat 
123456,00-001,nowy swiat,4,warszawa,, 

:

{'unit_no': '123456', 'kod': '00-001', 'street': 'nowy swiat', 'house': '4', 'city': 'warszawa', 'lng': 21.0220598, 'lat': 52.2302825} 

결과 : 이 output_file.csv 제로 크기 output_file.csv이 열립니다

에 아무것도 표시하지 않음 데이터

예상 출력 파일 : 내 마음에

unit_no,kod,street,house,city,lng,lat 
123456,00-001,nowy swiat,4,warszawa,21.0220598,52.2302825 

다음 단계는 생성 된 출력에서 ​​geojson 파일을 생성하고 그 후지도에 내 마커 또는 인구를 시각화.

귀중한 조언을 미리 보내 주셔서 감사합니다.

답변

0

것은 헤더 목록을 만들고 CSV의 헤더의 첫 번째 행을 만들 output_file.csv

header = ['unit_no','kod','street','house','city','lng','lat'] 
writer.writerow(header) 

이에 먼저 작성하고 출력 사전 CSV의 최초의 행마다 관한 것이며, 데이터를 추가 그에 상응하여.

+0

당신이 내 코드에 넣어 어디를 가리킬 수 있습니다. 그런 간단한 질문에 대해 유감스럽게 생각합니다. 내가 그것을 open ('output_file ....'섹션에 놓았을 때 반환 됨 : TypeError : 'str'이 아닌 바이트와 같은 객체가 필요함 – Krystian

+0

라이터를 정의한 직후. 또한 user_dict를 인쇄 한 후에 writer 명령을 사용하여 csv로 작성하십시오 (csv에 dict을 쓰지 못했습니다). –

+0

안녕 Vinay, 수정 후 게시물 코드에 대한 내 대답을 참조하십시오. 도와 주셔서 감사합니다. – Krystian

0

이 경우 당신이 아니라 내 코드는 다음과 같이해야 이해 :

with open('output_file.csv', 'wb') as fout: # output csv file 
    writer = csv.writer(fout) 
    header = ['unit_no','kod','street','house','city','lng','lat'] # your suggestion 
    writer.writerow(header) # your suggestion 
    with open('source_file.csv','r') as csvfile: # input csv file 

및 완료 :

 print (users_dict) 
     writer = csv.DictWriter(fout, delimiter=',') # your suggestion 
     writer.writerows(user_dict) # your suggestion 
     fout.close() 
관련 문제