Ho lavorato a un programma per recuperare le domande dallo stack overflow. Fino a ieri il programma stava lavorando bene, ma dal momento che oggi sto ottenendo l'erroreUnicodeEncodeError: il codec 'ascii' non può codificare il carattere u ' u201c' nella posizione 34: ordinale non compreso nell'intervallo (128)
"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)"
Attualmente vengono visualizzate le domande, ma mi sembra di essere in grado di copiare l'output in un nuovo file di testo.
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')
Quale linea sta causando questo errore? Inoltre, la regex '[aA-zZ] +' non farà quello che pensi che faccia. Hai bisogno di '[A-Za-z] +' o '(? I) [A-Z] +'. –
question.write (q.title) sta causando l'errore. –
Sto usando ** [aA-zZ] + ** per estrarre solo parole e ignorando numeri e caratteri speciali. –