Aar*_*ith 31 python pyscripter python-2.7 stackexchange-api
我一直在研究从堆栈溢出中检索问题的程序.直到昨天该程序工作正常,但从今天起我收到了错误
"Message File Name Line Position
Traceback
<module> C:\Users\DPT\Desktop\questions.py 13
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 34: ordinal not in range(128)"
Run Code Online (Sandbox Code Playgroud)
目前正在显示问题,但我似乎无法将输出复制到新的文本文件.
import sys
sys.path.append('.')
import stackexchange
so = stackexchange.Site(stackexchange.StackOverflow)
term= raw_input("Enter the keyword for Stack Exchange")
print 'Searching for %s...' % term,
sys.stdout.flush()
qs = so.search(intitle=term)
print '\r--- questions with "%s" in title ---' % (term)
for q in qs:
print '%8d %s' % (q.id, q.title)
with open('E:\questi.txt', 'a+') as question:
question.write(q.title)
time.sleep(10)
with open('E:\questi.txt') as intxt:
data = intxt.read()
regular = re.findall('[aA-zZ]+', data)
print(regular)
tokens = set(regular)
with open('D:\Dictionary.txt', 'r') as keywords:
keyset = set(keywords.read().split())
with open('D:\Questionmatches.txt', 'w') as matches:
for word in keyset:
if word in tokens:
matches.write(word + '\n')
Run Code Online (Sandbox Code Playgroud)
Tim*_*ker 53
q.title
是一个Unicode字符串.将其写入文件时,您需要先对其进行编码,最好是完全支持Unicode的编码,例如UTF-8
(如果不这样做,Python将默认使用ASCII
不支持上述任何字符代码点的编解码器127
).
question.write(q.title.encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
应该解决问题.
顺便说一下,程序在character “
(U+201C
)上跳了起来.