标签: python-unicode

Python unicode错误

有人可以解释一下为什么在下面的例子中,print a 引发异常,而a.__str__()不是吗?

>>> class A:
...   def __init__(self):
...     self.t1 = "?akovec".decode("utf-8")
...     self.t2 = "tg"
...   def __str__(self):
...     return self.t1 + self.t2
... 
>>> a = A()
>>> print a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u010d' in position 0: ordinal not in range(128)
>>> a.__str__()
u'\u010dakovectg'
>>> print a.__str__()
?akovectg
Run Code Online (Sandbox Code Playgroud)

python python-unicode

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

Django管理员不允许保存unicode slugs

我正试图为这个模型保存一个波斯slu ::

class Category(models.Model):
    name = models.CharField('name', max_length=100)
    slug = models.SlugField('slug', unique=True)
    description = models.TextField('description')

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    @permalink
    def get_absolute_url(self):
        return ('category_detail', None, {
            'slug': self.slug
        })

    def __unicode__(self):
        return u'%s' % self.name
Run Code Online (Sandbox Code Playgroud)

但Django没有保存页面和投诉:

Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens
Run Code Online (Sandbox Code Playgroud)

我也尝试使用这种保存方法

    def save(self, *args, **kwargs):
      self.name = slugify_unicode(self.name)
      super(Category, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

但它没有解决问题.所以我对此有所了解并感谢您帮助解决这个问题.

django django-admin python-unicode

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

如何使用Pythonstartswith方法组合unicode和ascii字符串?

就我而言,当我使用时,我将 Unicode text_string 和前缀作为 ASCII 字符串

text-string.startswith(prefix) 
Run Code Online (Sandbox Code Playgroud)

我以这种方式得到一个例外

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9: ordinal not in range(128)

如何比较两个字符串,我尝试使用unicode(string)将 ASCII 字符串转换为 Unicode方法但仍然遇到相同的异常。

如何解决这个问题?在最坏的情况下如何在比较时抑制此异常?

text - u'PreciChrom I/II is a lyophilized control based on human citrated plasma.'
prefix - 'Reagents – working solutions'
Run Code Online (Sandbox Code Playgroud)

python unicode ascii python-unicode

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

将 unicode 小写字母转换为其 ASCII 等效项

我有以下数据集

\n\n
\'F\xca\x80\xc9\xaa\xe1\xb4\x87\xc9\xb4\xe1\xb4\x85\',\n \'\xe1\xb4\x8d\xe1\xb4\x8f\xe1\xb4\x8d\',\n \'\xe1\xb4\x8d\xe1\xb4\x80\xe1\xb4\x8b\xe1\xb4\x87s\',\n \'\xca\x9c\xe1\xb4\x8f\xe1\xb4\x9c\xca\x80\xca\x9f\xca\x8f\',\n \'\xe1\xb4\x9b\xca\x9c\xe1\xb4\x87\',\n \'\xe1\xb4\x84\xe1\xb4\x8f\xe1\xb4\x8d\xe1\xb4\x98\xe1\xb4\x9c\xe1\xb4\x9b\xe1\xb4\x87\xca\x80\',\n \'\xca\x99\xe1\xb4\x87\xe1\xb4\x87\xc9\xb4\',\n \'\xe1\xb4\x8f\xe1\xb4\x9c\xe1\xb4\x9b\',\n \'\xe1\xb4\x80\',\n \'\xe1\xb4\x8a\xe1\xb4\x8f\xca\x99\',\n \'\xd2\x93\xe1\xb4\x8f\xca\x80\',\n \'\xe1\xb4\x8d\xe1\xb4\x8f\xc9\xb4\xe1\xb4\x9b\xca\x9cs\',\n \'\xca\x99\xe1\xb4\x9c\xe1\xb4\x9b\',\n \'\xca\x9f\xe1\xb4\x80s\xe1\xb4\x9b\',\n \'\xe1\xb4\x8d\xe1\xb4\x8f\xc9\xb4\xe1\xb4\x9b\xca\x9c\',\n \'\xca\x9c\xe1\xb4\x87\xca\x80\',\n \'\xe1\xb4\x84\xca\x9c\xe1\xb4\x87\xe1\xb4\x84\xe1\xb4\x8b\',\n \'\xe1\xb4\x8a\xe1\xb4\x9cs\xe1\xb4\x9b\',\n \'\xe1\xb4\xa1\xe1\xb4\x8f\xca\x80\xe1\xb4\x8b\xc9\xaa\xc9\xb4\xc9\xa2\',\n \'\xd2\x93\xe1\xb4\x87\xe1\xb4\xa1\',\n \'\xca\x9c\xe1\xb4\x8f\xe1\xb4\x9c\xca\x80s\',\n \'s\xe1\xb4\x8f\xe1\xb4\x9c\xca\x80\xe1\xb4\x84\xe1\xb4\x87\',\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想使用 Python 脚本转换成 ASCII 格式\n例如:

\n\n
F\xca\x80\xc9\xaa\xe1\xb4\x87\xc9\xb4\xe1\xb4\x85 - FRIEND\n\xe1\xb4\x8d\xe1\xb4\x8f\xe1\xb4\x8d - MOM\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已经尝试过编码解码,但这不起作用\我也尝试过这个解决方案。但这并不能解决我的问题。

\n

python unicode ascii python-unicode

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

一些烦人的字符没有被 unicodedata 规范化

我有一个如下所示的 python 字符串。该字符串来自一家美国上市公司向 SEC 提交的文件。我试图使用unicodedata.normalise函数从字符串中删除一些烦人的字符,但这并没有删除所有字符。这种行为背后的原因可能是什么?

from unicodedata import normalize
s = 'GTS.Client.Services@JPMChase.com\nFacsimile\nNo.:\xa0 312-233-2266\n\xa0\nJPMorgan Chase Bank,\nN.A., as Administrative Agent\n10 South Dearborn, Floor 7th\nIL1-0010\nChicago, IL 60603-2003\nAttention:\xa0 Hiral Patel\nFacsimile No.:\xa0 312-385-7096\n\xa0\nLadies and Gentlemen:\n\xa0\nReference is made to the\nCredit Agreement, dated as of May\xa07, 2010 (as the same may be amended,\nrestated, supplemented or otherwise modified from time to time, the \x93Credit Agreement\x94), by and among\nHawaiian Electric Industries,\xa0Inc., a Hawaii corporation (the \x93Borrower\x94), the Lenders from time to\ntime party thereto and JPMorgan Chase Bank, …
Run Code Online (Sandbox Code Playgroud)

python unicode unicode-normalization python-3.x python-unicode

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

为什么在控制台中运行Python脚本时会引发UnicodeDecodeError,而在Eclipse/PyDev中却没有?

我的脚本在Windows 8控制台中运行时会引发UnicodeDecodeError,但在Eclipse/PyDev中作为启动配置运行时则不会.PyDev环境和从unicode控制台运行python.exe的区别在哪里?

unicode pydev python-2.7 python-unicode

0
推荐指数
1
解决办法
558
查看次数

如何计算 unicode 字典的 MD5 校验和?

我在 python 中有一个字典,其中包含 unicode 值。我想计算这本字典的 md5 总和。我尝试使用这个问题的答案:Computing an md5 hash of a data structure

import hashlib
import bencode
data = {'unicode_text': '????'}
data_md5 = hashlib.md5(bencode.bencode(data)).hexdigest()
print data_md5
Run Code Online (Sandbox Code Playgroud)

但问题是bencode返回此错误:

KeyError: <type 'unicode'>
Run Code Online (Sandbox Code Playgroud)

python unicode dictionary python-unicode

0
推荐指数
1
解决办法
641
查看次数

python3脚本中可以使用哪些unicode字符?

一些 unicode 字符可用于命名变量、函数等,没有任何问题,例如?。

>>> ? = "Hello world!"
>>> print(?)
Hello world!
Run Code Online (Sandbox Code Playgroud)

其他 unicode 字符会引发错误,例如 ?。

>>> ? = "Hello world"
  File "<stdin>", line 1
    ?
    ^
SyntaxError: invalid character '?' (U+2207)
Run Code Online (Sandbox Code Playgroud)

哪些 unicode 字符可用于在 python 中形成有效的表达式?哪些 unicode 字符会引发 SyntaxError?

并且,是否有一种合理的方法来包含在 python 脚本中引发错误的 unicode 字符?我想用 ? 在计算梯度的函数名称中,例如 ?f 表示 f 的梯度。

python unicode python-3.x python-unicode

0
推荐指数
1
解决办法
32
查看次数

python读取包含\ x0a的文件,而不是python中的\\ x0a

我有xml文件,其中包含十六进制字符\ x0a.我想将它们转换为适当的unicode字符,如python中的\n.

每当我尝试读取文件时,它都会逃避反斜杠字符.

例如,我的文件内容是

get EtqLt5fwmRBE\x0a
Run Code Online (Sandbox Code Playgroud)

然后在读取文件之后,字符串的表示就出现了

get EtqLt5fwmRBE\\x0a
Run Code Online (Sandbox Code Playgroud)

但我想要的是转换\x0a\n

\x0a文件中没有.还有其他角色.例如repr(),文件中的一行是

\\x7c12\\x7c5\\x7c\\x0a
Run Code Online (Sandbox Code Playgroud)

上面的预期产出是

|12|5|
Run Code Online (Sandbox Code Playgroud)

python unicode python-unicode

-1
推荐指数
1
解决办法
1154
查看次数

Python - isalpha() 在 unicode 修饰符上返回 True

u'\\u02c7'.isalpha()如果符号\xcb\x87不是字母,为什么返回 True?此方法仅适用于 ASCII 字符吗?

\n

python unicode python-unicode

-1
推荐指数
1
解决办法
1328
查看次数

在 Python 中读取带有法语字符的文件

我想在 Python 中读取一个包含法语字符的文件,例如“é”。我正在使用这些代码行来做到这一点:

import codecs
with codecs.open(r'C:\Users\chsafouane\Desktop\saf.txt', encoding='ascii') as f:
    for line in f.readlines():
        line 
Run Code Online (Sandbox Code Playgroud)

然而,我得到一个

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 3: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

为了重现该错误,我尝试读取的文件仅包含一个词:“加速”。有没有办法做到这一点?

python file python-unicode

-1
推荐指数
1
解决办法
8117
查看次数

在python中替换反斜杠'\'

尝试在python中替换'\'时,数据更改并给我未知的字母。

我已经尝试过string.replace,re.sub,regex_replace

a = '70\123456'
b = '70\123\456'

a = a.replace('\\','-')
b = b.replace('\\','-')
Run Code Online (Sandbox Code Playgroud)

预期结果:

a = '70-123456'
b = '70-123-456'
Run Code Online (Sandbox Code Playgroud)

但是实际结果是:

a = 70S456
b = 70S?
Run Code Online (Sandbox Code Playgroud)

有什么问题以及如何解决?

python replace python-unicode

-1
推荐指数
1
解决办法
68
查看次数

AttributeError:'unicode'对象没有属性XXX

我正在尝试使用pipl.我想从邮件中获取信息并将其保存到Excel文件中.

from piplapis.search import SearchAPIRequest
from piplapis.search import SearchAPIError
from openpyxl import load_workbook

request = SearchAPIRequest(email=u'clark.kent@example.com', api_key='......')

try:
    response = request.send()
except SearchAPIError as e:
    print e.http_status_code, e

filename = "mails.xlsx"
wb = load_workbook(filename = filename)
ws = wb['Sheet1']
Run Code Online (Sandbox Code Playgroud)

print(response.raw_json) 回报

{
    "@http_status_code": 200,
    "@visible_sources": 3,
    "@available_sources": 3,
    "@persons_count": 1,
    "@search_id": "0",
    "query": {
        "emails": [
            {
                "address": "clark.kent@example.com",
                "address_md5": "2610ee49440fe757e3cc4e46e5b40819"
            }
        ]
    },
    "available_data": {
        "premium": {
            "relationships": 6,
            "usernames": 2,
            "jobs": 3,
            "addresses": 2,
            "ethnicities": 3, …
Run Code Online (Sandbox Code Playgroud)

python json python-2.7 python-unicode

-5
推荐指数
1
解决办法
2604
查看次数