# -*- coding: utf-8 -*-
# coding: utf-8
import gc
import os
import sys
import json
import re
import chardet
#替换文本的特殊字符
def ReplaceFun(string=""):
string2 = string.replace('\\', '').replace('^', '')
list = re.findall('.*Name_trd": "(.*)", "Name_eng".*', string2)
for rename in list:
string2 = string2.replace(rename, rename.replace("\"", ""))
list = re.findall('.*Name_chn": "(.*)", "Name_trd".*', string2)
for rename in list:
string2 = string2.replace(rename, rename.replace("\"", ""))
list = re.findall('.*Name_eng": "(.*)", "X_coord".*', string2)
for rename in list:
string2 = string2.replace(rename, rename.replace("\"", " "))
return string2
#转换
def ChuLi(file_name=""):
inf = open(file_name, 'r', errors='ignore')
string = inf.read() # .encode(encoding).decode(encoding).encode('utf8')
inf.close()
del inf
outf = open(out_name, 'w', encoding="utf8")
outf.write(ReplaceFun(string))
outf.close()
del outf
del string
path = sys.path[0]
print(path)
##file_path = "D:\全国导航1.4加密_秒转度_国测局坐标_成果\Road13Geojson"
file_path = path # "D:\全国导航1.4加密_秒转度_国测局坐标_成果\WaterGeojson"
files = [fname for fname in os.listdir(file_path) if fname[-8:] == ".geojson"]
index = 0
for file in files:
file_name = file_path + '\\' + file
out_name = file_path + '\\out\\' + file
#f = open(file_name, 'rb')
print(str(index) + ",<<<<<<<<<<<<<<<<<<<<<<<" + file + "======== 开始")
ChuLi(file_name)
# print(str(result["encoding"]) + "=====其他格式====")
index += 1
'''ff = f.read()
f.close()
result = chardet.detect(ff[0:5000] + ff[-1:-5000])
if str(result["encoding"]) == "ascii":
encoding = result["encoding"]
print(encoding)
inf = open(file_name, 'r', errors='ignore')
string = inf.read() # .encode(encoding).decode(encoding).encode('utf8')
inf.close()
del inf
print(string[0:1000] + string[-1:1000])
print(type(string[0:1000] + string[-1:1000]))
outf = open(out_name, 'w', encoding="utf8")
outf.write(ReplaceFun(string))
outf.close()
del outf
del string
elif str(result["encoding"]) == "ISO-8859-1":
encoding = result["encoding"]
print(encoding)
inf = open(file_name, 'r', errors='ignore')
string = inf.read() # .encode(encoding).decode(encoding).encode('utf8')
inf.close()
del inf
print(string[0:1000] + string[-1:1000])
print(type(string[0:1000] + string[-1:1000]))
outf = open(out_name, 'w', encoding="utf8")
outf.write(ReplaceFun(string))
outf.close()
del outf
elif str(result["encoding"]) == "GB2312":
encoding = result["encoding"]
print(encoding)
inf = open(file_name, 'r', errors='ignore')
string = inf.read() # .encode(encoding).decode(encoding).encode('utf8')
inf.close()
del inf
print(string[0:1000] + string[-1:1000])
print(type(string[0:1000] + string[-1:1000]))
outf = open(out_name, 'w', encoding="utf8")
outf.write(ReplaceFun(string))
outf.close()
del outf
elif str(result["encoding"]) == "UTF-8-SIG":
encoding = result["encoding"]
print(encoding)
inf = open(file_name, 'r', encoding='utf-8-sig', errors='ignore')
string = inf.read() # .encode(encoding).decode(encoding).encode('utf8')
inf.close()
del inf
print(string[0:1000] + string[-1:1000])
print(type(string[0:1000] + string[-1:1000]))
outf = open(out_name, 'w', encoding="utf8")
outf.write(ReplaceFun(string))
outf.close()
del outf
else:
encoding = result["encoding"]
print(encoding)
inf = open(file_name, 'r', errors='ignore')
string = inf.read() # .encode(encoding).decode(encoding).encode('utf8')
inf.close()
del inf
print(string[0:1000] + string[-1:1000])
print(type(string[0:1000] + string[-1:1000]))
outf = open(out_name, 'w', encoding="utf8")
outf.write(ReplaceFun(string))
outf.close()
del outf
'''
del ff
gc.collect()
# 开始读取并转换编码
保存本地py文件到需要转换的文件夹,自动获取本级所有geojson并转换为utf-8
mkdir out
python toutf8.py