草庐IT

multi_byte_str

全部标签

Python:_ ("str") 是做什么的?

我在Django源代码中看到了这个:description=_("Comma-separatedintegers")description=_("Date(withouttime)")它有什么作用?我在Python3.1.3中尝试它但失败了:>>>foo=_("bar")Traceback(mostrecentcalllast):File"",line1,infoo=_("bar")NameError:name'_'isnotdefined2.4.4也不走运:>>>foo=_("bar")Traceback(mostrecentcalllast):File"",line1,in-top

Python:__str__,但对于一个类,而不是一个实例?

我理解以下Python代码:>>>classA(object):...def__str__(self):...return"AninstanceoftheclassA"...>>>>>>a=A()>>>printaAninstanceoftheclassA现在,我想更改的输出>>>printA我需要重载哪个函数才能做到这一点?即使从未实例化该类,该解决方案也必须有效。Python2.x和3中的情况是否不同? 最佳答案 在元类上定义__str__():classA(object):class__metaclass__(type):de

python - 'str' 对象在 Python3 中没有属性 'decode'

我对python3.3.4中的“解码”方法有一些问题。这是我的代码:forlinesinopen('file','r'):decodedLine=lines.decode('ISO-8859-1')line=decodedLine.split('\t')但是我无法解码这个问题的行:AttributeError:'str'objecthasnoattribute'decode'你有什么想法吗?谢谢 最佳答案 一个编码字符串,一个解码字节。您应该从文件中读取字节并对其进行解码:forlinesinopen('file','rb'):de

python - Python 的 str.rstrip() 函数中的错误,还是我自己的愚蠢?

要么这是一个错误,要么我即将学习一些关于Python行为方式的新知识。:)我有一个充满键/值对的字典。每个键都有一个唯一的前缀,ias_XX_XX_。我正在尝试获取字典中每个唯一前缀的列表。首先,我得到一个以'_x1'结尾的所有键的列表。接下来,我使用rstrip('_x1')从它们中去除所有的'_x1'。除了最后一个ias_1_1_x1之外,这对它们都适用。它不再被剥离为ias_1_1,而是变为ias_。运行代码自己看看:d={'ias_16_10_x2':575,'ias_16_10_x1':0,'ias_16_10_y1':0,'ias_16_10_y2':359,'ias_16

python - Python 中 str 的静态方法与实例方法

所以,我了解到字符串有一个center方法。>>>'a'.center(3)'a'然后我注意到我可以使用类型的“str”对象做同样的事情,因为>>>type(str)使用这个“类型”对象,我可以像访问静态函数一样访问字符串方法。>>>str.center('a',5)'a'唉!这违反了python的禅宗。应该有一种——最好只有一种——显而易见的方法来做到这一点。这两种方法连类型都不一样。>>>type(str.center)>>>type('Ni!'.center)现在,这是应该如何设计python类的示例吗?为什么类型不同?什么是method_descriptor,我为什么要费心?感

Python 错误 : null byte in input prompt

我发现了input('some\x00text')将提示输入some而不是sometext。从源代码中,我发现这个函数使用了C函数PyOS_Readline,它忽略了NULL字节后提示中的所有内容。来自PyOS_StdioReadline(FILE*sys_stdin,FILE*sys_stdout,constchar*prompt):fprintf(stderr,"%s",prompt);https://github.com/python/cpython/blob/3.6/Python/bltinmodule.c#L1989https://github.com/python/cpyt

python - Recurrentshop 和 Keras : multi-dimensional RNN results in a dimensions mismatch error

我对Recurrentshop和Keras有疑问。我正在尝试在循环模型中使用连接和多维张量,但无论我如何安排输入、形状和batch_shape,我都会遇到维度问题。最少的代码:fromkeras.layersimport*fromkeras.modelsimport*fromrecurrentshopimport*fromkeras.layersimportConcatenateinput_shape=(128,128,3)x_t=Input(shape=(128,128,3,))h_tm1=Input(shape=(128,128,3,))h_t1=Concatenate()([x_

python - sqlalchemy:alembic 批量插入失败: 'str' 对象没有属性 '_autoincrement_column'

我的模型看起来像classCategory(UserMixin,db.Model):__tablename__='categories'uuid=Column('uuid',GUID(),default=uuid.uuid4,primary_key=True,unique=True)name=Column('name',String,nullable=False)parent=Column('parent',String,nullable=False)created_on=Column('created_on',sa.types.DateTime(timezone=True),defa

python - 使用pyinstaller时出错: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff

我在使用pyinstaller编译PyQt代码时遇到问题。我用这一行来编译:c:\Anaconda3\Scripts\pyinstaller.exe-y-F--distpath="."MyQt.py然后我收到此错误消息:File"c:\anaconda36bis\lib\site-packages\PyInstaller\hooks\hook-zmq.py",line18,inhiddenimports.extend(collect_submodules('zmq.backend'))File"c:\anaconda36bis\lib\site-packages\PyInstaller

Python3 写入 gzip 文件 - 内存 View : a bytes-like object is required, 不是 'str'

我想写一个文件。根据文件的名称,这可能会或可能不会被gzip模块压缩。这是我的代码:importgzipfilename='output.gz'opener=gzip.openiffilename.endswith('.gz')elseopenwithopener(filename,'wb')asfd:print('blahblahblah'.encode(),file=fd)我正在以二进制模式打开可写文件并对要写入的字符串进行编码。但是我收到以下错误:File"/usr/lib/python3.5/gzip.py",line258,inwritedata=memoryview(dat