我想在下一行以小写字符开头时用空格替换换行符。在
我的python3.3代码在下一行以[a-z]开头时可以工作,但如果以(小写)Unicode字符开头,则会失败。在
Test file (saved as UTF-8): Test to check<CRLF>whether it's working.<CRLF>Aquela é<CRLF>árvore pequena.<CRLF>
import os
input_file = 'test.txt'
input_file_path = os.path.join("c:", "\\", "Users", "Paulo", "workspace", "pdf_to_text", input_file)
input_string = open(input_file_path).read()
print(input_string)
import re
pattern = r'\n([a-zàáâãäåæçčèéêëěìíîïłðñńòóôõöøőřśŝšùúûüůýÿżžÞ]+)'
pattern_obj = re.compile(pattern)
replacement_string = " \\1"
output_string = pattern_obj.sub(replacement_string, input_string)
print(output_string)`
您的实际问题与regex无关。您正在使用不正确的拉丁1编码读取utf-8文本。在
要读取utf-8文件:
^{pr2}$关于regex的旧答案(与OPs问题无关):
一般来说,单个用户感知的字符,如
ç
,é
可能跨越多个Unicode码位,因此[çé]
可以分别匹配这些Unicode码元,而不是匹配整个字符。(?:ç|é)
可以解决这个问题,还有其他问题,例如Unicode规范化(NFC,NFKD)。在regex
模块支持POSIX字符类[:lower:]
:要使用
re
模块模拟[:lower:]
类:结果是一样的。在
相关问题 更多 >
编程相关推荐