-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstationCodes.py
36 lines (27 loc) · 997 Bytes
/
stationCodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import re
import json
import const
from APIs import API
from tool import Utility
class StationCodes(object):
def saveStationCodes(self,session):
# 若文件存在,则直接return
if os.path.exists(const.stationCodesFilePath):
return
res = session.get(API.stationCode)
stations = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)',res.text)
print(stations)
# 注意编码格式utf-8
with open(const.stationCodesFilePath, 'w', encoding='utf-8') as f:
# ensure_ascii = False 防止乱码
f.write(json.dumps(dict(stations),ensure_ascii = False))
def getCodesDict(self):
with open(const.stationCodesFilePath, 'r', encoding='utf-8') as file:
dict = json.load(file)
return dict
def getStations(self):
return self.getCodesDict().keys()
if __name__ == '__main__':
StationCodes.saveStationCodes(Utility().session)
StationCodes().getStations()