python中文单词_用Python定义单词

在尝试从dictionary.reference.com抓取单词定义时,遇到了类型错误。问题出在将字节对象转换为字符串进行正则表达式匹配上。解决方案是在使用`re.findall`之前先用`decode()`方法解码输出。代码中存在HTML片段,可能需要进一步清理以正确解析内容。
摘要由CSDN通过智能技术生成

然而,这并不是因为我试图在我的代码中实现这个答案(对那个线程的操作有效,但对我不起作用)。在

我的职能是:def define_word(user_define_input):

srch = str(user_define_input[1])

output_word=urllib.request.urlopen("http://dictionary.reference.com/browse/"+srch+"?s=t")

output_word=output_word.read()

items=re.findall('

for output_word in items:

y=output_word.replace('','')

m=re.findall('at Dictionary.com, a free online dictionary with pronunciation, synonyms and translation. Look it up now! "/>',z)

if m==[]:

if z.startswith("Get your reference question answered by Ask.com"):

print ("Word not found!")

else:

print (z)

else:

print ("Word not found!")

注:

^{pr2}$

另外,这包含了一点HTML:/抱歉,但这是另一个答案使用的。在

所以,当我尝试使用这个错误时:File "/Users/******/GitHub/Multitool/functions.py", line 104, in define_word

items=re.findall('

File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/re.py", line 210, in findall

return _compile(pattern, flags).findall(string)

TypeError: can't use a string pattern on a bytes-like object

注:

第104行函数.py是:items=re.findall('

第210行复制是此函数的最后一行:def findall(pattern, string, flags=0):

"""Return a list of all non-overlapping matches in the string.

If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern

has more than one group.

Empty matches are included in the result."""

return _compile(pattern, flags).findall(string) #line 210

如果这里面有什么不清楚的地方,请告诉我(我不确定要为这个添加什么标签:/)。提前感谢您:)您可以随意更改任何内容,甚至重新编写整个内容,但一定要使用变量/列表:define_word(函数名)

用户定义输入

添加:output_word = output_word.decode()

或者改变output_word = output_word.read().decode('iso-8859-2')

在输入以下内容时给出:定义测试:Test definition, the means by which the presence, quality, or genuineness of anything is determined; a means of trial.

Word not found!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值