Skip to main content
 首页 » 编程设计

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

2024年10月01日33cyq1162

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

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

例如:会出现字符串\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' 

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