![](/img/trans.png)
[英]Python: How do I remove text between two delimiters WITHOUT IMPORTING MODULE
[英]How to remove text between two delimiters in Python
我正在尝试删除短语“segmentation”之后 [] 括号之间的所有文本:“请参阅文件中的以下片段以了解上下文。
"annotations": [
{
"id": 1,
"image_id": 1,
"segmentation": [
[
621.63,
1085.67,
621.63,
1344.71,
841.66,
1344.71,
841.66,
1085.67
]
],
"iscrowd": 0,
"bbox": [
621.63,
1085.67,
220.02999999999997,
259.03999999999996
],
"area": 56996,
"category_id": 1124044
},
{
"id": 2,
"image_id": 1,
"segmentation": [
[
887.62,
1355.7,
887.62,
1615.54,
1114.64,
1615.54,
1114.64,
1355.7
]
],
"iscrowd": 0,
"bbox": [
887.62,
1355.7,
227.0200000000001,
259.8399999999999
],
"area": 58988,
"category_id": 1124044
},
{
"id": 3,
"image_id": 1,
"segmentation": [
[
1157.61,
1411.84,
1157.61,
1661.63,
1404.89,
1661.63,
1404.89,
1411.84
]
],
"iscrowd": 0,
"bbox": [
1157.61,
1411.84,
247.2800000000002,
249.7900000000002
],
"area": 61768,
"category_id": 1124044
},
........... and so on.....
我最终只想在出现分词后删除方括号之间的所有文本。 换句话说,output 看起来像(对于第一个实例):
"annotations": [
{
"id": 1,
"image_id": 1,
"segmentation": [],
"iscrowd": 0,
"bbox": [
621.63,
1085.67,
220.02999999999997,
259.03999999999996
],
"area": 56996,
"category_id": 1124044
},
我试过使用下面的代码,但目前运气不太好。 由于新线路,我有什么问题吗?
import re
f = open('samplfile.json')
text = f.read()
f.close()
clean = re.sub('"segmentation":(.*)\]', '', text)
print(clean)
f = open('cleanedfile.json', 'w')
f.write(clean)
f.close()
我明白我对干净行中 [s 的确切定位可能不太正确,但这段代码目前没有删除任何内容。
Python 有一个内置的json
模块,用于解析和修改 JSON。正则表达式可能很脆弱,而且比它的价值更让人头疼。
您可以执行以下操作:
import json
with open('samplfile.json') as input_file, open('output.json', 'w') as output_file:
data = json.load(input_file)
for i in range(len(data['annotations'])):
data['annotations'][i]['segmentation'] = []
json.dump(data, output_file, indent=4)
然后, output.json
包含:
{
"annotations": [
{
"id": 1,
"image_id": 1,
"segmentation": [],
"iscrowd": 0,
"bbox": [
621.63,
1085.67,
220.02999999999997,
259.03999999999996
],
"area": 56996,
"category_id": 1124044
},
{
"id": 2,
"image_id": 1,
"segmentation": [],
"iscrowd": 0,
"bbox": [
887.62,
1355.7,
227.0200000000001,
259.8399999999999
],
"area": 58988,
"category_id": 1124044
},
{
"id": 3,
"image_id": 1,
"segmentation": [],
"iscrowd": 0,
"bbox": [
1157.61,
1411.84,
247.2800000000002,
249.7900000000002
],
"area": 61768,
"category_id": 1124044
}
]
}
您的方法大部分是正确的,但 Python 正则表达式不接受\n
作为.
, 要修复它,请在 re.sub() 中添加flags=re.DOTALL
作为参数。
顺便说一下,您可能需要在正则表达式中使用\"
而不是"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.