python3批量转geojson文本编码为utf-8

# -*- 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

捣net菜菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值