Unicode编码错误:'ascii'编解码器无法编码字符u'\u201c',在位置34:序数不在范围(128)内

40 投票
2 回答
85645 浏览
提问于 2025-04-18 10:04

我一直在做一个程序,用来从Stack Overflow上获取问题。直到昨天,这个程序运行得很好,但从今天开始,我遇到了一个错误

"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)"

现在问题是能正常显示出来,但我好像无法把输出内容复制到一个新的文本文件里。

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')

2 个回答

3

我在使用Transifex的API时也遇到了这个问题。

response['source_string']

出现了一个错误:UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 3: ordinal not in range(128)

我通过使用response['source_string'].encode("utf-8")来解决了这个问题。

import requests

username = "api"
password = "PASSWORD"

AUTH = (username, password)

url = 'https://www.transifex.com/api/2/project/project-site/resource/name-of-resource/translation/en/strings/?details'

response = requests.get(url, auth=AUTH).json()

print response['key'], response['context']
print response['source_string'].encode("utf-8")
66

q.title 是一个Unicode字符串。当你想把它写入文件时,首先需要对它进行编码,最好使用一种支持所有Unicode字符的编码方式,比如 UTF-8(如果不这样做,Python会默认使用 ASCII 编码,而这种编码只支持127个字符以上的字符都无法处理)。

question.write(q.title.encode("utf-8"))

这样应该能解决问题。

顺便提一下,程序在处理字符 U+201C)时出现了问题。

撰写回答