我有一个数据帧,其中一列包含 ObjectId 列表的字符串表示形式。 即:
"[ObjectId('5d28938629fe749c7c12b6e3'), ObjectId('5caf4522a30528e3458b4579')]"
我想将其从字符串文字转换为仅包含 id 的 python 列表,例如:
['5d28938629fe749c7c12b6e3', '5caf4522a30528e3458b4579']
json.loads
和 ast.literal_eval
都失败了,因为字符串包含ObjectId
我分享这个正则表达式:https://regex101.com/r/m5rW2q/1
您可以单击代码生成器,例如:
import re
regex = r"ObjectId('(w+)')"
test_str = "[ObjectId('5d28938629fe749c7c12b6e3'), ObjectId('5caf4522a30528e3458b4579')]"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
输出:
Match 1 was found at 1-37: ObjectId('5d28938629fe749c7c12b6e3')
Group 1 found at 11-35: 5d28938629fe749c7c12b6e3
Match 2 was found at 39-75: ObjectId('5caf4522a30528e3458b4579')
Group 1 found at 49-73: 5caf4522a30528e3458b4579
对于您的示例:
import re
regex = r"ObjectId('(w+)')"
test_str = "[ObjectId('5d28938629fe749c7c12b6e3'), ObjectId('5caf4522a30528e3458b4579')]"
matches = re.finditer(regex, test_str, re.MULTILINE)
[i.groups()[0] for i in matches]
输出:
['5d28938629fe749c7c12b6e3', '5caf4522a30528e3458b4579']
所有关于正则表达式的信息,你可以在这里找到:https://docs.python.org/3/library/re.html
好吧,你可以使用 替换
a = "[ObjectId('5d28938629fe749c7c12b6e3'), ObjectId('5caf4522a30528e3458b4579')]"
a.replace('ObjectId(', '').replace(")","")
#Output:
"['5d28938629fe749c7c12b6e3', '5caf4522a30528e3458b4579']"
找到行;在 '处拆分;从列表中选择项目 1 和 3:
my_df.loc[my_df["my_column"].str.contains("ObjectId"),"my_column"].str.split("'")[0][1:4:2]
精确地给出两个元素的列表:
['5d28938629fe749c7c12b6e3', '5caf4522a30528e3458b4579']