Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “解决问题的方法”;UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\u2013';在第16位:序号不在范围内(128)“;它不起作用了_Python_String_Python 2.7 - Fatal编程技术网

Python “解决问题的方法”;UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\u2013';在第16位:序号不在范围内(128)“;它不起作用了

Python “解决问题的方法”;UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\u2013';在第16位:序号不在范围内(128)“;它不起作用了,python,string,python-2.7,Python,String,Python 2.7,将字符串写入文件时出错 UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 16: ordinal not in range(128) 问题是我已经把绳子清理干净了。但我不知道为什么这不起作用 这是我清理字符串的代码 import string replace_punctuation = string.maketrans(string.punctuation, ' '*len(strin

将字符串写入文件时出错

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 16: ordinal not in range(128)
问题是我已经把绳子清理干净了。但我不知道为什么这不起作用

这是我清理字符串的代码

import string

replace_punctuation = string.maketrans(string.punctuation, ' '*len(string.punctuation))

def clean_text(text):
    try:
        text = text.decode('utf-8', 'ignore')
    except:
        text = text.encode('utf-8', 'ignore')
        text = text.decode('utf-8', 'ignore')
    text = text.encode('ascii', 'ignore').lower().translate(replace_punctuation)
    text = " ".join(text.split())
    return text

text = "some text with special characters"
text = clean_text(text)
#outfile is an output file
outfile.write(text) #This step is giving error
你知道我清理绳子时遗漏了什么吗

我知道这个问题被问了很多。但我的问题是,这个问题最常见的解决方案是

text.encode('utf-8')
这对我不起作用

我也试过了

text.encode('utf-8', 'ignore')

不起作用

不确定它为您抛出了什么错误。我刚刚在windows上的python 2.7.10中测试了这一点:

# -*- coding: utf8 -*-
import string

replace_punctuation = string.maketrans(string.punctuation, ' '*len(string.punctuation))

def clean_text(text):
    try:
        text = text.decode('utf-8', 'ignore')
    except:
        text = text.encode('utf-8', 'ignore')
        text = text.decode('utf-8', 'ignore')
    text = text.encode('ascii', 'ignore').lower().translate(replace_punctuation)
    text = " ".join(text.split())
    return text

text = "some text öäööäwith special characters"
text = clean_text(text)
print text
with open('test.txt','w') as outfile:
    outfile.write(text)

>>>
some text with special characters
该文件还包含相同的文本

这可能是另一种选择:

from string import ascii_letters, whitespace, punctuation, digits
text = ''.join(c for c in text if c in chain(ascii_letters, whitespace, punctuation, digits)).lower().translate(replace_punctuation)

您可以过滤掉非ascii格式的字符,因此不必担心unicode。

您希望将编码文本写入文件,而不是解码的unicode对象。在使用
Ä
对unicode对象调用
encode('ascii')
时,它将失败,因为它不是ascii格式。另外:不要担心,每个人都会在python中经历这些。精彩的演讲:@SebastianWozny:谢谢你,伙计。那么解决这个问题的办法是什么呢?删除使用ascii忽略的编码?附言:现在就看那个视频。:)