一.csv转geojson

1.将带有点坐标的 csv 转 geojson, 不带其他属性

"""
将带有点坐标的 csv 转 geojson, 不带其他属性
"""
import csvcsv_file = open('E:/test.csv', encoding='gbk')
csv_reader = csv.reader(csv_file)geojson = '{"type":"FeatureCollection","features":['
for index, row in enumerate(csv_reader):print(row[0])geojson += '{"geometry":{"coordinates":[%f,%f],"type":"Point"},"properties":{},"type":"Feature"},' % (float(row[0]),float(row[1]))geojson = geojson[:-1] + ']}'
links_file = open('test.geojson', 'w')
links_file.write(geojson)

2.将带有点坐标的 csv 转 geojson, 带其他属性

"""
将带有点坐标的 csv 转 geojson, 带其他属性
"""
import csv
import jsoncsv_file = open('test.csv', encoding='utf8')
csv_reader = csv.reader(csv_file)geojson = '{"type":"FeatureCollection","features":['lon_index = 0
lat_index = 1
for index, row in enumerate(csv_reader):if index == 0:header = rowcontinueproperty_dict = {}for indexJ, j in enumerate(row):if indexJ == lon_index or indexJ == lat_index:continueproperty_dict[header[indexJ]] = jgeojson += '{"geometry":{"coordinates":[%f,%f],"type":"Point"},"properties":%s,"type":"Feature"},' \% (float(row[lon_index]), float(row[lat_index]), json.dumps(property_dict, ensure_ascii=False))geojson = geojson[:-1] + ']}'
links_file = open('test.geojson', 'w', encoding='utf8')
links_file.write(geojson)

二.geojson转geojson

geojson转geojson, 并加上一个随机数

"""
geojson转geojson, 并加上一个随机数
"""
import json
import randomfile_object = open("D:/random_points.geojson", "r+", encoding='utf8')
file_data_str = file_object.read()
file_object.close()origin_data = json.loads(file_data_str)geojson = '{"type":"FeatureCollection","features":['
for i in origin_data["features"]:properties = i["properties"]properties["counts"] = random.randint(0, 20000)geojson += '{"geometry":{"coordinates":%s,"type":"Point"},"properties":%s,"type":"Feature"},' % (json.dumps(i["geometry"]["coordinates"]), json.dumps(i["properties"], ensure_ascii=False))geojson = geojson[:-1] + ']}'
links_file = open('D:/random_points_value.geojson', 'w', encoding='utf8')
links_file.write(geojson)

三.csv转json

"""
将 csv 转为 json
"""
import csv
import jsoncsv_file = open('F:/test.csv', encoding='utf8')
csv_reader = csv.reader(csv_file)list_data = []header = []
for index, row in enumerate(csv_reader):if index == 0:header = rowelse:one_json = {}for index_j, j in enumerate(row):one_json[header[index_j]] = row[index_j]list_data.append(one_json)json_data = {"data": list_data}json_file = open('F:/test.json', 'w', encoding='utf8')
json_file.write(json.dumps(json_data, ensure_ascii=False))

四.json转geojson

1.读取含有点坐标的json文件, 并转为geojson

"""
读取含有点坐标的json文件, 并转为geojson
"""
import jsonfile_object = open("D:/收费员位置高德.json", "r+", encoding='utf8')
file_data_str = file_object.read()
file_object.close()origin_data = json.loads(file_data_str)geojson = '{"type":"FeatureCollection","features":['
for i in origin_data["data"]:geojson += '{"geometry":{"coordinates":[%s,%s],"type":"Point"},"properties":{},"type":"Feature"},' % (i["longitude"], i["latitude"])geojson = geojson[:-1] + ']}'
links_file = open('D:/收费员位置高德.geojson', 'w', encoding='utf8')
links_file.write(geojson)

2.逐行读取含有点坐标的json文件, 并逐行写入geojson

"""
逐行读取含有点坐标的json文件, 并逐行写入geojson中
"""
import jsonjson_str = ""
for line in open("E:/income_province_evsmc.json"):json_str += linejson_dict = json.loads(json_str)file_writer = open("E:/points.geojson", "a+")geojson = '{"type":"FeatureCollection","features":['
file_writer.write(geojson)
for index, i in enumerate(json_dict["result"]):coords = "%f,%f" % (i["lon"], i["lat"])file_writer.write('{"geometry": {"type": "Point", "coordinates": [%s]},"properties": {}}' % coords)if index != len(json_dict["result"]) - 1:file_writer.write(',')file_writer.write(']}')

五.文本符处理

1.读取csv,将换行符处理后再插入另一个csv

"""
读取csv,将换行符处理后再插入另一个csv
"""import csvcsv_file = open("test.csv", encoding='utf-8')
csv_reader_lines = csv.reader(csv_file)file_object = open("abc.csv", 'w', newline='', encoding='utf-8')
writer = csv.writer(file_object)for index, i in enumerate(csv_reader_lines):i[4] = i[4].replace(" ", "").replace("\r", "").replace("\n", "")writer.writerow(i)file_object.close()

2.去除txt文件中的各种转义符

"""
去除txt文件中的各种转义符
"""
read_object = open("polygon.geojson", "r+", encoding="utf8")
file_data_str = read_object.read()
read_object.close()data_str = file_data_str.replace("\n", "").replace("\r", "").replace(" ", "").replace(" ", "")
txt_file = open("polygon_format.geojson", 'w', encoding="utf8")
txt_file.write(data_str)
txt_file.close()

Python文件格式转换相关推荐

  1. python 文件格式转换_数据分析:基于Python的自定义文件格式转换系统

    ( 白宁超 2018年7月16日14:47:41 ) 导读:随着大数据的快速发展,自然语言处理.数据挖掘.机器学习技术应用愈加广泛.针对大数据的预处理工作是一项庞杂.棘手的工作.首先数据采集和存储,尤 ...

  2. python 文件格式转换_python实现txt文件格式转换为arff格式

    本文实例为大家分享了python实现txt文件格式转换为arff格式的具体代码,供大家参考,具体内容如下 将文件读取出来的时候默认都是字符型的,所以有转换出来有点问题,但是还是可以用的. 文件要求第一 ...

  3. python 文件格式转换_Python的处理数据,如何进行数据转换,学会三种方式

    平时我们在处理数据的时候,有些数据类型不是我们想要的,怎么办? 如: python数据转换 num01,num02是str类型,但是我们需要的是整型,所以通过int转换成了整数. 数据转换 如何完成数 ...

  4. python实现文件格式转换_python实现快速文件格式批量转换的方法

    用python实现文件夹下的成批文件格式转换 我们对于文件转换的需求很大,甚至于对于图片的格式,JPG和PNG格式在肉眼看来都没什么差别,但是对于计算机而言,它有时候就只接受这些肉眼看起来差不多的格式 ...

  5. python 文件批量转换格式_使用python批量化音乐文件格式转换的实例

    使用python批量化音乐文件格式转换的实例 最近在做声音文件数据处理,写了一个自动将m4a文件转化为wav的脚本. import os m4a_path = "/Users/Downloa ...

  6. Windows应用程序文件格式转换控件LEADTOOLS ePrint Professional

    2019独角兽企业重金招聘Python工程师标准>>> LEADTOOLS ePrint Professional控件是一个多功能一体化文件转换解决方案,它可帮助您将任何Window ...

  7. python 图像格式转换_如何用六行Python构建图像类型转换器

    python 图像格式转换 by AMR 通过AMR 如何用六行Python构建图像类型转换器 (How to build an image type convertor in six lines o ...

  8. python批量读取图片并复制入word_提取出 Word 文档里的图片 并利用 python 批量转换格式...

    日常工作中,你是否遇到过这样的场景,领导发来一份 Word 文档,要求你将文档中的图片存储到一个文件夹内,并且还要将图片都改成 .jpg 或者 .png,你会怎么办?你是不是一边内心崩溃,一边开始一张 ...

  9. python批量音频转格式_GitHub - shede333/SWConvertVideoToAudio: Python批量转换 视频 为 音频MP3(即提取音频文件)...

    Python批量转换 视频 为 音频MP3(即提取音频文件) 输入文件格式:ffmpeg支持的视频文件 输出格式格式:mp3文件 使用方法: 注意:使用前需要先安装 ffmpeg 才行(Python最 ...

最新文章

  1. mysql基于时间盲注_MYSQL基于时间的盲注详解
  2. python异步io多文件_Python 异步 IO 性能又上一层楼
  3. 一分钟安装IDA pro7.0
  4. objective-c 通过类名实例化类
  5. python网络编程爬虫_Python爬虫--网络编程
  6. 件测试专家分享III GUI自动化测试相关
  7. MyBaties入门
  8. linux/centos shell脚本中非交互式修改密码
  9. 如何使用JavaWeb实现户籍管理系统?
  10. html弹窗复制,js复制弹窗美化
  11. 有道云笔记 markdown html,有道云笔记Markdown之甘特图
  12. 随机生成三位密码,然后穷举法破解密码
  13. matlab中如何分布运行,matlab安装、运行与其他问题集锦
  14. 2022建筑电工(建筑特殊工种)考试题目模拟考试平台操作
  15. 线索二叉树(中序、先序和后序及遍历)
  16. 什么人不在生死簿_高人亲眼所见的“地狱、生死簿、三世因果”(转)阴间一直是世...
  17. 如何在互联网公司求职成功
  18. php轮播插件,移动端h5轮播插件swipe实例详解
  19. pandas用众数填充缺失值_数据处理之缺失值填充
  20. nvm最强安装配置教程

热门文章

  1. 加强版斐波那契数列(矩阵快速幂)
  2. 抖音爆款短视频背后的拍摄技巧。丨国仁网络资讯
  3. python基础教程第二版修订版下册答案_Python基础教程(第2版 修订版)
  4. 想着学习前端开发3个月就月薪十几k的人,给你提出5个建议
  5. unity 清理项目中的多余资源
  6. 一文带你了解 MQTT 协议(连接 ONE-NET平台)
  7. 因为相信所以看见,既然看见注定坚信《3》
  8. (转)css怎么一个DIV盒子同时插多张张背景图片
  9. 他工作10年,老板却让他走人
  10. iOS开发之iOS13 暗黑模式(Dark Mode)适配