
前提條件
- 本文是個(gè)人使用Python處理文件的電子筆記,由于水平有限,難免出現(xiàn)錯(cuò)漏,敬請批評改正。
- 更多精彩內(nèi)容,可點(diǎn)擊進(jìn)入我的個(gè)人主頁查看
相關(guān)介紹
- 熟悉Python
實(shí)驗(yàn)環(huán)境
- Python是一種跨平臺的計(jì)算機(jī)程序設(shè)計(jì)語言。是一個(gè)高層次的結(jié)合了解釋性、編譯性、互動性和面向?qū)ο蟮哪_本語言。最初被設(shè)計(jì)用于編寫自動化腳本(shell),隨著版本的不斷更新和語言新功能的添加,越多被用于獨(dú)立的、大型項(xiàng)目的開發(fā)。
- Python OS模塊是負(fù)責(zé)程序與操作系統(tǒng)的交互,提供了訪問操作系統(tǒng)底層的接口和非常豐富的方法用來處理文件和目錄。
Python修改labelme的Json文件
- Python 3.x (面向?qū)ο蟮母呒壵Z言)
{"version": "5.0.1",
"flags": { },
"shapes": [
{ "label": "1",
"points": [
[
4713.666666666667,
2783.666666666667
],
[
6087.0,
3983.666666666667
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {
}
}
],
"imagePath": "1.bmp",
"imageData": null,
"imageHeight": 4000,
"imageWidth": 8320
}代碼實(shí)現(xiàn)以修改"imagePath"的內(nèi)容為例,代碼如下。
import os
import json
def alter_json(in_json_path,out_json_path):
file_in = open(in_json_path, "r", encoding='utf-8')
# json.load數(shù)據(jù)到變量json_data
json_data = json.load(file_in)
# 修改json中的imagePath數(shù)據(jù)內(nèi)容
json_data["imagePath"] = json_data['imagePath'][:-4] + '.jpg'
file_in.close()
# 創(chuàng)建一個(gè)寫文件
file_out = open(out_json_path, "w", encoding='utf-8')
# 將修改后的數(shù)據(jù)寫入文件
file_out.write(json.dumps(json_data))
file_out.close()
if __name__=="__main__":
in_json_path = './origin_json/1.json' # 原始json所在文件夾
out_json_path = './output_json/1.json' # 新json輸出文件夾
alter_json(in_json_path,out_json_path)輸出結(jié)果{"version": "5.0.1",
"flags": { },
"shapes": [
{ "label": "1",
"points": [
[
4713.666666666667,
2783.666666666667
],
[
6087.0,
3983.666666666667
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {
}
}
],
"imagePath": "1.jpg",
"imageData": null,
"imageHeight": 4000,
"imageWidth": 8320
}更多精彩內(nèi)容,可點(diǎn)擊進(jìn)入Python日常小操作專欄或我的個(gè)人主頁查看
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前名稱:Python修改labelme的Json文件-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://chinadenli.net/article36/cddpsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、App設(shè)計(jì)、網(wǎng)站營銷、自適應(yīng)網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容