所以我出现了以 \u
开头的字符串,后跟各种形式的 4 字符十六进制 (它们不是 unicode 对象,而是数据中的实际字符串,这就是为什么我会想要清理数据) 并希望用空格替换该出现的地方。
示例文本文件:Hello\u2022已创建、审核、\u00e9执行和维护
例如:会出现字符串\u2022
和\u00e9
,我想找到\u
并将其连同其后的 4 个字符子字符串 2022
和 00e9
一起删除。我正在寻找适合这种模式的正则表达式。
示例代码:
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'
仍然希望查看您的代码示例,以便我们提供更准确的答案
关于python - 特定模式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561314/