WONGDEEM

Untitled

Oct 16th, 2021
758
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2.  
  3. postals = []
  4. latitudes = []
  5. longitudes = []
  6. def string_to_html(string):
  7.     string.replace(" ", "+")
  8.     return string
  9.  
  10.  
  11. def append_to_file(string):
  12.     with open("postal.csv", "a") as text_file:
  13.         print(string, file=text_file)
  14.        
  15.    
  16. for i in range(len(resale_data)):
  17.     sys.stdout.write('\r')
  18.     # the exact output you're looking for:
  19.     sys.stdout.write(f"{i}/{len(resale_data)}")
  20.     sys.stdout.flush()
  21.     address = f'{resale_data["street_name"][i]} {resale_data["block"][i]}'
  22.     url = f'https://developers.onemap.sg/commonapi/search?searchVal={string_to_html(address)}&returnGeom=Y&getAddrDetails=Y&pageNum=1'
  23.  
  24.     x = requests.get(url).json()
  25.     if len(x["results"]) == 0:
  26.         postals.append(None)
  27.         latitudes.append(None)
  28.         longitudes.append(None)
  29.         append_to_file("None,None,None")
  30.         continue
  31.     postals.append(x["results"][0]["POSTAL"])
  32.     latitudes.append(x["results"][0]["LATITUDE"])
  33.     longitudes.append(x["results"][0]["LONGITUDE"])
  34.     append_to_file(f'{x["results"][0]["POSTAL"]},{x["results"][0]["LATITUDE"]},{x["results"][0]["LONGITUDE"]}')
  35. resale_data["postal"] = postals
  36. resale_data["latitude"] = latitudes
  37. resale_data["longitude"] = longitudes
  38.  
  39. def string_to_html_mrt(string):
  40.     string.replace(" ", "+")
  41.     return string+"+MRT+STATION"
  42. mrt_stations_name = []
  43. mrt_stations_postal = []
  44. mrt_stations_long = []
  45. mrt_stations_lat = []
  46. mrt_stations = pd.read_csv("mrt_stations.csv",header=None)
  47.  
  48. def append_to_file_mrt(string):
  49.     with open("mrt_info.csv", "a") as text_file:
  50.         print(string, file=text_file)
  51. print(mrt_stations)
  52.  
  53. for i in mrt_stations[0]:
  54.     url = f'https://developers.onemap.sg/commonapi/search?searchVal={string_to_html_mrt(i)}&returnGeom=Y&getAddrDetails=Y&pageNum=1'
  55.     x = requests.get(url).json()
  56. #     print(x["results"][])
  57.     for j in x["results"]:
  58.         print(j["SEARCHVAL"])
  59.         if "MRT STATION" in j["SEARCHVAL"]:
  60.             mrt_stations_name.append(j["SEARCHVAL"])
  61.             mrt_stations_postal.append(j["POSTAL"])
  62.             mrt_stations_long.append(j["LONGITUDE"])
  63.             mrt_stations_lat.append(j["LATITUDE"])
  64.             append_to_file_mrt(f'{j["SEARCHVAL"]},{j["POSTAL"]},{j["LONGITUDE"]},{j["LATITUDE"]}')
  65.             break
RAW Paste Data