gpt4 book ai didi

python - Unicode编码错误: 'ascii' codec can't encode character u'\u2013' in position 7

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:09 33 4
gpt4 key购买 nike

对于以下代码;

phonenumbers = ['(209) 525-2987', '509-477-4598', None, '229-259–1234']
phoneCheck = re.compile('^[1-9]\d{2}-\d{3}-\d{4}$')

for pn in phonenumbers:
print pn
if phoneCheck.match(str(pn)):
print 'Matched!'
else:
print 'Not Matched!'

我在结果中收到此错误,我相信这与电话号码中使用的短划线类型错误有关,我该如何更正此错误以将其标记为“不匹配”?

(209) 576-6546
Not Matched!
509-477-6726
Not Matched!
None
Not Matched!
229-259–9756
Runtime error
Traceback (most recent call last):
File "<string>", line 6, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 7: ordinal not in range(128)

最佳答案

您的诊断是正确的。 (最后一个电话号码中的第二个破折号是某种奇特的破折号,我敢打赌您是从文字处理程序或电子表格中复制并粘贴电话号码的。无论如何......)

这里有一个快速简单的方法:安装 unidecode 包,然后:

import re
import warnings

import unidecode

dash = u'\u2013'
phonenumbers = ['(209) 525-2987', '509-477-4598', None, '229-259' + dash + '1234']
phoneCheck = re.compile('^[1-9]\d{2}-\d{3}-\d{4}$')

# if you pass an ascii string into unidecode, it will complain, but still work.
# Just catch the warnings.
with warnings.catch_warnings():
warnings.simplefilter("ignore")

for pn in phonenumbers:
print pn

# if pn is None, it's not a phone number (and None will cause unidecode
# to throw an error)
if pn and phoneCheck.match(unidecode.unidecode(pn)):
print 'Matched!'
else:
print 'Not Matched!'

关于python - Unicode编码错误: 'ascii' codec can't encode character u'\u2013' in position 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43054673/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com