gpt4 book ai didi

python - 特定模式的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:09 26 4
gpt4 key购买 nike

所以我出现了以 \u 开头的字符串,后跟各种形式的 4 字符十六进制 (它们不是 unicode 对象,而是数据中的实际字符串,这就是为什么我会想清理数据) 并想用空格替换那些出现的地方。

示例文本文件:Hello\u2022 Created, reviewed,\u00e9executed and maintained

例如:会出现字符串\u2022\u00e9,我想找到\u 并将其连同 4 个字符的子字符串 202200e9 一起删除。我正在为这种模式寻找合适的正则表达式。

示例代码:

import json
import io
import re

files = glob('Candidate Profile Data/*')

for file_ in files:
with io.open(file_, 'r', encoding='us-ascii') as json_file:
json_data = json_file.read().decode()
json_data = re.sub('[^\x00-\x7F]+',' ',json_data)
json_data = json_data.replace('\\n',' ')
json_data = re.sub(r'\\u[0-9a-f]{,4}',' ',json_data)

print json_data
json_data = json.loads(json_data)
print(json_data)

最佳答案

确实,我们需要您的代码示例,但作为指针,我认为您需要的正则表达式类似于 r'\\u[0-9a-f]{,4}'

这是一个使用示例:

>>> import re
>>> my_string='Hello \\u2022 Created, reviewed, \\u00e9executed and maintained'
>>> my_string
'Hello \\u2022 Created, reviewed, \\u00e9executed and maintained'
>>> re.sub(r'\\u[0-9a-f]{,4}',"",my_string)
'Hello Created, reviewed, executed and maintained'

仍然希望看到您的 CODE 示例,以便我们提供更准确的答案

关于python - 特定模式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com