0
이 스크립트를 완전히 독립적 인 방법으로 리팩토링 할 수 있습니까?리팩토링 파이썬 스크립트를 분리 가능한 방법으로
import json
import requests
from collections import defaultdict
from pprint import pprint
def hasNumbers(inputString):
return any(char.isdigit() for char in inputString)
# open up the output of 'data-processing.py'
with open('job-numbers-by-location.txt') as data_file:
# print the output to a file
with open('phase_ii_output.txt', 'w') as output_file_:
for line in data_file:
identifier, name, coords, number_of_jobs = line.split("|")
coords = coords[1:-1]
lat, lng = coords.split(",")
# print("lat: " + lat, "lng: " + lng)
response = requests.get("http://api.geonames.org/countrySubdivisionJSON?lat="+lat+"&lng="+lng+"&username=s.matthew.english").json()
codes = response.get('codes', [])
for code in codes:
if code.get('type') == 'ISO3166-2':
country_code = '{}-{}'.format(response.get('countryCode', 'UNKNOWN'), code.get('code', 'UNKNOWN'))
if not hasNumbers(country_code):
# print("code: " + country_code + ", jobs: " + number_of_jobs)
output_file_.write("code: " + country_code + ", jobs: " + number_of_jobs)
output_file_.close()
나는 그것을 훨씬 더 큰 프로세스의 구성 요소로 포함시킬 수 있도록 노력해 왔습니다.
나는 결국 [이 일] (http://stackoverflow.com/questions/40914419/parse-json-from-an-api-with-python-exception-handling-에 통합하기 위해 노력하고있어 외과 적 추출) –