草庐IT

utf8_encoded_str

全部标签

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 UTF-16 CSV 阅读器

我有一个必须阅读的UTF-16CSV文件。Pythoncsv模块似乎不支持UTF-16。我正在使用python2.7.2。我需要解析的CSV文件很大,有几GB的数据。下面是JohnMachin问题的答案printrepr(open('test.csv','rb').read(100))输出内容只有abc的test.csv'\xff\xfea\x00b\x00c\x00'我认为csv文件是在美国的Windows机器上创建的。我正在使用MacOSXLion。如果我使用phihag提供的代码和包含一条记录的test.csv。使用的示例test.csv内容。下面是printrepr(open(

python - 统一码编码错误 : 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

当我运行我的代码时,我得到这个错误:UserId="{}".format(source[1])UnicodeEncodeError:'ascii'codeccan'tencodecharactersinposition0-3:ordinalnotinrange(128)我的代码是:defview_menu(type,source,parameters):ADMINFILE='static/users.txt'fp=open(ADMINFILE,'r')users=ast.literal_eval(fp.read())ifnotparameters:ifnotsource[1]inuse

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

python - 'utf- 8' codec can' t 解码字节 0xa0 在位置 4276 : invalid start byte

我尝试读取并打印以下文件:txt.tsv(https://www.sec.gov/files/dera/data/financial-statement-and-notes-data-sets/2017q3_notes.zip)根据SEC,数据集以单一编码提供,如下所示:TabDelimitedValue(.txt):utf-8,tab-delimited,\n-terminatedlines,withthefirstlinecontainingthefieldnamesinlowercase.我当前的代码:importcsvwithopen('txt.tsv')astsvfile:r

python - str.format() -> 如何左对齐

>>>print'thereare{0:10}studentsand{1:10}teachers'.format(scnt,tcnt)thereare100studentsand20teachers输出的代码是什么:thereare100studentsand20teachers谢谢。 最佳答案 print'thereare{0:虽然老%运算符(operator)使用-为了对齐,新的format方法使用和> 关于python-str.format()->如何左对齐,我们在StackOve