从字符串中动态删除子字符串
Dynamically remove substring from string
我有一个有点直截了当的困境,但我一直无法找到解决方案。
我正在从 MongoDB 数据库中获取一个 JSON 文件并使用 json.dumps 将其转换为字符串:
client = MongoClient("XXXX")
db = client.testdb
table = db.testingtable
document = table.find()
document = list(document)
docs = json.dumps(document,default=str)
print(docs)
当前输出:
[{"_id": "67", "userId": 167, "productID": "Reference('product_id', ObjectId('5f4523b893d7b757bcbd2d71'))"}]
我将如何动态地 在整个字符串中搜索单词 Reference
,如果找到,它应该删除除括号内的字母数字值之外的所有内容 没有这样做:
docs.replace("Reference('product_id', ObjectId('",'').replace("'))",'')
预期输出:
[{"_id": "67", "userId": 167, "productID": "5f4523b893d7b757bcbd2d71"}]
from pandas.io.json import json_normalize
df=pd.json_normalize([{"_id": "67", "userId": 167, "productID": "Reference('product_id', ObjectId('5f4523b893d7b757bcbd2d71'))"}])#convert to df
#Using regex, extract string between `ObjectId(`and `)` and turn it back to json
df.assign(productID=df.productID.str.extract('(?<=ObjectId\()(.*?)(?=\))')).to_json(orient="records")
[{"_id":"67","userId":167,"productID":"'5f4523b893d7b757bcbd2d71'"}]
我有一个有点直截了当的困境,但我一直无法找到解决方案。
我正在从 MongoDB 数据库中获取一个 JSON 文件并使用 json.dumps 将其转换为字符串:
client = MongoClient("XXXX")
db = client.testdb
table = db.testingtable
document = table.find()
document = list(document)
docs = json.dumps(document,default=str)
print(docs)
当前输出:
[{"_id": "67", "userId": 167, "productID": "Reference('product_id', ObjectId('5f4523b893d7b757bcbd2d71'))"}]
我将如何动态地 在整个字符串中搜索单词 Reference
,如果找到,它应该删除除括号内的字母数字值之外的所有内容 没有这样做:
docs.replace("Reference('product_id', ObjectId('",'').replace("'))",'')
预期输出:
[{"_id": "67", "userId": 167, "productID": "5f4523b893d7b757bcbd2d71"}]
from pandas.io.json import json_normalize
df=pd.json_normalize([{"_id": "67", "userId": 167, "productID": "Reference('product_id', ObjectId('5f4523b893d7b757bcbd2d71'))"}])#convert to df
#Using regex, extract string between `ObjectId(`and `)` and turn it back to json
df.assign(productID=df.productID.str.extract('(?<=ObjectId\()(.*?)(?=\))')).to_json(orient="records")
[{"_id":"67","userId":167,"productID":"'5f4523b893d7b757bcbd2d71'"}]