有人可以解释一下为什么在下面的例子中,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) 我正试图为这个模型保存一个波斯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)
但它没有解决问题.所以我对此有所了解并感谢您帮助解决这个问题.
就我而言,当我使用时,我将 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) 我有以下数据集
\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\',\nRun Code Online (Sandbox Code Playgroud)\n\n我想使用 Python 脚本转换成 ASCII 格式\n例如:
\n\nF\xca\x80\xc9\xaa\xe1\xb4\x87\xc9\xb4\xe1\xb4\x85 - FRIEND\n\xe1\xb4\x8d\xe1\xb4\x8f\xe1\xb4\x8d - MOM\nRun Code Online (Sandbox Code Playgroud)\n\n我已经尝试过编码解码,但这不起作用\我也尝试过这个解决方案。但这并不能解决我的问题。
\n我有一个如下所示的 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
我的脚本在Windows 8控制台中运行时会引发UnicodeDecodeError,但在Eclipse/PyDev中作为启动配置运行时则不会.PyDev环境和从unicode控制台运行python.exe的区别在哪里?
我在 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) 一些 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 的梯度。
我有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) u'\\u02c7'.isalpha()如果符号\xcb\x87不是字母,为什么返回 True?此方法仅适用于 ASCII 字符吗?
我想在 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中替换'\'时,数据更改并给我未知的字母。
我已经尝试过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)
有什么问题以及如何解决?
我正在尝试使用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-unicode ×13
python ×11
unicode ×8
ascii ×2
python-2.7 ×2
python-3.x ×2
dictionary ×1
django ×1
django-admin ×1
file ×1
json ×1
pydev ×1
replace ×1