Python檔案格式轉換

2021-09-27 11:20:16 字數 4066 閱讀 8453

"""

將帶有點座標的 csv 轉 geojson, 不帶其他屬性

"""import csv

csv_file = open('e:/test.csv', encoding='gbk')

csv_reader = csv.reader(csv_file)

geojson = ',"properties":{},"type":"feature"},' % (float(row[0]),float(row[1]))

geojson = geojson[:-1] + ']}'

links_file = open('test.geojson', 'w')

links_file.write(geojson)

"""

將帶有點座標的 csv 轉 geojson, 帶其他屬性

"""import csv

import json

csv_file = open('test.csv', encoding='utf8')

csv_reader = csv.reader(csv_file)

geojson = '

for indexj, j in enumerate(row):

if indexj == lon_index or indexj == lat_index:

continue

property_dict[header[indexj]] = j

geojson += ',"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, 並加上乙個隨機數

"""import json

import random

file_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 = ',"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

"""import csv

import json

csv_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 = row

else:

one_json = {}

for index_j, j in enumerate(row):

one_json[header[index_j]] = row[index_j]

json_data =

json_file = open('f:/test.json', 'w', encoding='utf8')

json_file.write(json.dumps(json_data, ensure_ascii=false))

"""

讀取含有點座標的json檔案, 並轉為geojson

"""import json

file_object = open("d:/收費員位置高德.json", "r+", encoding='utf8')

file_data_str = file_object.read()

file_object.close()

origin_data = json.loads(file_data_str)

geojson = ',"properties":{},"type":"feature"},' % (

i["longitude"], i["latitude"])

geojson = geojson[:-1] + ']}'

links_file = open('d:/收費員位置高德.geojson', 'w', encoding='utf8')

links_file.write(geojson)

"""

逐行讀取含有點座標的json檔案, 並逐行寫入geojson中

"""import json

json_str = ""

for line in open("e:/income_province_evsmc.json"):

json_str += line

json_dict = json.loads(json_str)

file_writer = open("e:/points.geojson", "a+")

geojson = ',"properties": {}}' % coords)

if index != len(json_dict["result"]) - 1:

file_writer.write(',')

file_writer.write(']}')

"""

讀取csv,將換行符處理後再插入另乙個csv

"""import csv

csv_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()

"""

去除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()

DOS檔案格式 與 UNIX檔案格式 轉換

這裡闡述兩種方法 1.使用vim的方法 在vim下 使用如下命令 set fileformat unix 就可轉換成 unix 檔案格式,也可使用其縮寫形式命令 set ff unix 直接輸入命令 set fileformat 或 set ff 還可查詢當前的命令格式。2.使用linux命令工具 ...

用iconv 檔案格式轉換

bin bash files file grep iso 8859 cut d f 1 for i in echo files doiconv f gbk t utf 8 i tmp mv tmp i done windows轉到linux中,檔案亂碼,檔案編碼轉換 最近,學習又重新開始linux學...

檔案格式轉換神器 pandoc

by francis hao mar 11,2017 如果你需要在各種型別的檔案中穿梭,那麼你需要這把瑞士軍刀 pandoc 它可以將各種常見的不常見的檔案型別轉換成另一種,我感興趣的是在linux下將markdown檔案轉換成html,當然這不成問題。如果需要檢視全部支援的轉換格式,見 參考1 既...