相关疑难解决方法(0)

Python:为unicode清理一个字符串?

可能重复:
Python UnicodeDecodeError - 我是否误解编码?

我有一个字符串,我正在尝试为该unicode()函数安全:

>>> s = " foo “bar bar ” weasel"
>>> s.encode('utf-8', 'ignore')

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    s.encode('utf-8', 'ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 5: ordinal not in range(128)
>>> unicode(s)

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    unicode(s)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 5: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我大部分时间都在这里挥舞着.如何从字符串中删除不安全的字符需要做什么?

与这个问题有点相关,虽然我无法从中解决我的问题.

这也失败了: …

python unicode character-encoding

14
推荐指数
2
解决办法
2万
查看次数

Selenium webdriver和unicode

这是我第二天使用Selenium 2库,而Unicode的痛苦似乎永远不会消退.

我只是做最基本的操作,想要打印页面源:

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("http://google.com")

print driver.page_source
Run Code Online (Sandbox Code Playgroud)

果然,我收到一个错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0119' in position 62045:  
ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我怎么能把它编码到utf-8

python unicode selenium

12
推荐指数
1
解决办法
2万
查看次数

为什么Windows上的python 2.7在打印时需要一个空格才能打开unicode字符?

我使用cmd Windows,chcp 65001,这是我的代码:

print u'\u0110 \u0110' + '\n'
Run Code Online (Sandbox Code Playgroud)

结果:

 (a character cmd can't display) (character what i want)
 Traceback (most recent call last):
      File "b.py", line 26, in <module>
        print u'\u0110 \u0110'
    IOError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)

但是,当我使用这段代码时:

print u' \u0110 \u0110' + '\n'
Run Code Online (Sandbox Code Playgroud)

结果:

(a space)(charecter what i want) (character what i want)
Traceback (most recent call last):
  File "b.py", line 26, in <module>
    print u' \u0110 \u0110' + '\n'
IOError: [Errno 2] No such file …
Run Code Online (Sandbox Code Playgroud)

python unicode python-2.7

6
推荐指数
2
解决办法
1150
查看次数

Python:无法写入文件 - UnicodeEncodeError

此代码应该将一些文本写入文件。当我尝试将文本写入控制台时,一切正常。但是当我尝试将文本写入文件时,出现 UnicodeEncodeError。我知道,这是一个常见的问题,可以使用正确的解码或编码来解决,但我尝试过它仍然得到相同的 UnicodeEncodeError。我究竟做错了什么?

我附上了一个例子。

print "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".decode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2])
Run Code Online (Sandbox Code Playgroud)

(StarBuy sro、Inzertujte 的照片、auto-moto、oble?enie、reality、prácu、zvieratá、starožitnosti、dovolenky、nábytok、všetko pre deti、obuv、stroj....

with open("test.txt","wb") as f:
   f.write("(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".decode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2]))
Run Code Online (Sandbox Code Playgroud)

UnicodeEncodeError: 'ascii' 编解码器无法对位置 50 中的字符 u'\u010d' 进行编码:序号不在范围内 (128)

问题可能出在哪里?

python unicode encoding

2
推荐指数
1
解决办法
4099
查看次数