Python正则表达式和Excel
我试图使用openpyxl对从excel到python数组的数据执行常规匹配,但是数据是unicode的,python总是给出“无”。希伯来语和i-whant格式的数据用于将excel中的字符串转换为可以使用正则表达式匹配的字符串。。可以做些什么Python正则表达式和Excel,python,regex,hebrew,Python,Regex,Hebrew,我试图使用openpyxl对从excel到python数组的数据执行常规匹配,但是数据是unicode的,python总是给出“无”。希伯来语和i-whant格式的数据用于将excel中的字符串转换为可以使用正则表达式匹配的字符串。。可以做些什么 import re from openpyxl import load_workbook file_name = 'excel.xlsx' wb = load_workbook(file_name) ws = wb[u'beta'] li = []
import re
from openpyxl import load_workbook
file_name = 'excel.xlsx'
wb = load_workbook(file_name)
ws = wb[u'beta']
li = []
li2 = []
#readin the cells from excel into an array
for i in range(1,1500):
li2.append(ws["A"+str(i)].value)
for i in li2:
if i != None:
li.append(i)
#deliting the unwanted list for making memory
del li2
r = re.match("א",li[1])
r == None
>>> True
想要的结果是r.string=“somthing…”,而不是r==None
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
>>> li[1]
u"\u05d0\u05d1\u05d5 \u05d2'\u05d5\u05d5\u05d9\u05d9\u05e2\u05d3 (\u05e9\u05d1\u05d8)"
>>> print li[1]
אבו ג'ווייעד (שבט)
>>> r = re.match(u'א',li[1])
>>> r ==None
True
>>> r = re.match(ur'א',li[1])
>>> r = re.match(u'',li[1])
>>> r.string
u"\u05d0\u05d1\u05d5 \u05d2'\u05d5\u05d5\u05d9\u05d9\u05e2\u05d3 (\u05e9\u05d1\u05d8)"
>>> unicode('א')
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
unicode('א')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
>>> u'א'
u'\xe0'
>>> u'א'.encode("utf8")
'\xc3\xa0'
>>> u"א"
u'\xe0'
>>>
win32上的Python 2.7.9(默认,2014年12月10日12:24:55)[MSC v.1500 32位(英特尔)]
有关详细信息,请键入“copyright”、“credits”或“license()”。
>>>=================================================重新启动================================
>>>
>>>李[1]
u“\u05d0\u05d1\u05d5\u05d2'\u05d5\u05d5\u05d9\u05d9\u05e2\u05d3(\u05e9\u05d1\u05d8)”
>>>打印李[1]
אבו ג'ווייעד (שבט)
>>>r=重新匹配(u'א',li[1])
>>>r==无
真的
>>>r=重新匹配(ur'א',li[1])
>>>r=re.match(u'',li[1])
>>>右弦
u“\u05d0\u05d1\u05d5\u05d2'\u05d5\u05d5\u05d9\u05d9\u05e2\u05d3(\u05e9\u05d1\u05d8)”
>>>unicode(‘א’)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
unicode(‘א’)
UnicodeDecodeError:“ascii”编解码器无法解码位置0中的字节0xe0:序号不在范围内(128)
>>>u‘א’
u'\xe0'
>>>u'א'。编码(“utf8”)
“\xc3\xa0”
>>>u“א”
u'\xe0'
>>>
我将代码中指定的希伯来文字母放入几个单元格,然后运行以下代码:
# -*- coding: utf-8 -*-
import re
from openpyxl import load_workbook
file_name = 'worksheet.xlsx'
wb = load_workbook(file_name)
ws = wb[u'beta']
li = []
li2 = []
#readin the cells from excel into an array
for i in range(1,1500):
li2.append(ws["A"+str(i)].value)
for i in li2:
if i != None:
li.append(i)
#deliting the unwonted list for clearing memory
del li2
print "Non-empty cells: "
print li
r = re.search(u"א", li[1])
print "Match in: "
print r.string.encode('utf-8')
print "Position: "
print r.span()
输出:
Non-empty cells:
[u'Hebrew letter test 1 \u05d0', u'Hebrew letter test 2 \u05d0', u'Hebrew letter test 3 \u05d0', u'Hebrew letter test 4 \u05d0']
Match in:
Hebrew letter test 2 ÎÉ
Position:
(21, 22)
如果您需要这些,请告诉我。答案是:
import re
from openpyxl import load_workbook
file_name = "excel.xlsx"
wb = load_workbook(file_name)
ws = wb[wb.get_sheet_names()[0]]
#regex
match = re.search(r"\d",ws["A2"].value )
print match.group(0)
:)把你的代码和可能出现的错误放出来,如果你解释一下为什么这是答案,那么这个答案可能会重复,这样会更好。