我正在尝试用Python处理中文文本和大数据。 工作的一部分是从一些不需要的数据中清除文本。为此,我使用regex。但是,我遇到了一些问题,比如Python regex和PyCharm应用程序:
1)数据存储在postgresql中,在列中可以很好地查看,但是在选择并将其拉到var之后,它显示为一个正方形:
当打印到控制台的值如下所示:
薄荷糖100g
所以我认为应用程序编码没有问题,但是编码的调试部分没有问题,但是,我没有找到任何解决这种行为的方法。在
2)我需要注意的regex示例是删除中括号之间的值包括它们。我使用的代码是:
#!/usr/bin/env python
# -*- coding: utf-8 -*
import re
from pprint import pprint
import sys, locale, os
columnString = row[columnName]
startFrom = valuestoremove["startsTo"]
endWith = valuestoremove["endsAt"]
isInclude = valuestoremove["include"]
escapeCharsRegex = re.compile('([\.\^\$\*\+\?\(\)\[\{\|])')
nonASCIIregex = re.compile('([^\x00-\x7F])')
if escapeCharsRegex.match(startFrom):
startFrom = re.escape(startFrom)
if escapeCharsRegex.match(endWith):
endWith = re.escape(endWith)
if isInclude:
regex = startFrom + '(.*)' + endWith
else:
regex = '(?<=' + startFrom + ').*?(?=' + endWith + ')'
if nonASCIIregex.match(regex):
p = re.compile(ur'' + regex)
else:
p = re.compile(regex)
row[columnName] = p.sub("", columnString).strip()
但是regex不影响给定的字符串。 我用下一个代码做了一个测试:
^{pr2}$对我来说很好。 这两个代码示例之间的唯一区别是,n第一个regex值来自带有json的txt文件,编码为utf-8:
{
"between": {
"startsTo": "(",
"endsAt": ")",
"include": true,
"sequenceID": "1"
}
}, {
"between": {
"startsTo": "(",
"endsAt": ")",
"include": true,
"sequenceID": "2"
}
},{
"between": {
"startsTo": "(",
"endsAt": ")",
"include": true,
"sequenceID": "2"
}
},{
"between": {
"startsTo": "(",
"endsAt": ")",
"include": true,
"sequenceID": "2"
}
}
文件中的中文方括号也被视为方形:
我无法为这种行为找到解释或任何解决办法,因此社区需要帮助
谢谢你的帮助。在
经过多次搜索和协商,这里有了一个解决中文文本(也有混合语言和非混合语言)的方法
如您所见,我们将任何regex编码为utf-8,因此postgresql db值与regex匹配。在
问题是,您正在阅读的文本没有被正确地理解为Unicode(这是促使python3k进行彻底更改的一个大问题)。而不是:
你需要告诉它解码文件:
^{pr2}$然后继续使用
json.loads
等,它应该可以很好地工作。或者相关问题 更多 >
编程相关推荐