osc/research/nuofang-db/us-base/us-base-baidu-parser.py

60 lines
1.8 KiB
Python
Raw Normal View History

2025-05-28 19:16:17 +08:00
import json
import pymysql
txt_file_path = r"E:\yuxin\us-base-baidu.txt"
def get_deg_dec(deg_str):
deg_num = deg_str[:-1]
dir_str = deg_str[-1:]
sign = (-1, 1)[dir_str in ['N', 'E']]
i_degree = deg_num.find("°")
i_minute = deg_num.find("'")
i_second = deg_num.find('"')
degree = float(deg_num[:i_degree])
minute = float(deg_num[i_degree + 1:i_minute])
second = float(deg_num[i_minute + 1:i_second])
deg_dec_num = (degree + minute / 60 + second / 3600) * sign
return deg_dec_num
if __name__ == "__main__":
txt_file = open(txt_file_path, 'r', encoding='utf-8')
txt_file_content = txt_file.read()
txt_file.close()
db = pymysql.connect(host='39.101.194.63', port=23306,
user='root', passwd='passok123A', db='nfm', charset='utf8mb4')
cursor = db.cursor()
count = 0
for txt_line in txt_file_content.split('\n'):
count += 1
line_tuple = txt_line.split()
cn_name = line_tuple[0]
lat_str = line_tuple[1]
lon_str = line_tuple[2]
lat = get_deg_dec(lat_str)
lon = get_deg_dec(lon_str)
item = {
'id': count,
'cn_name': cn_name,
'lat_str': lat_str,
'lon_str': lon_str,
'lat': lat,
'lon': lon
}
cols = []
vals = []
for k in item:
v = item[k]
cols.append(k)
if type(v) == str:
vals.append("'{}'".format(v.replace("'", "\\'")))
else:
vals.append("{}".format(v))
SQL_INSERT = "INSERT INTO nfm.us_base_bd_info ({}) VALUES ({})".format(", ".join(cols), ", ".join(vals))
print("[No. {}]".format(count))
cursor.execute(SQL_INSERT)
db.commit()
db.close()