1

请注意:

CPython:

PS Z:\dev\poc\SDR> python
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from zipfile import ZipFile
>>> z=ZipFile('d:/aaa.zip')
>>> input=z.open(z.namelist()[0])
>>> next(input)
'aaa,bbb\n'
>>> next(input)
'123,456\n'
>>>

铁蟒:

PS Z:\dev\poc\SDR> ipy64
IronPython 2.7.1 (2.7.0.40) on .NET 4.0.30319.225
Type "help", "copyright", "credits" or "license" for more information.
>>> from zipfile import ZipFile
>>> z=ZipFile('d:/aaa.zip')
>>> input=z.open(z.namelist()[0])
>>> next(input)
b'aaa,bbb\n'
>>> next(input)
'123,456\n'
>>>

请注意 IronPython 显示第一行,b'aaa,bbb\n'而 CPython显示'aaa,bbb\n'. 这种差异非常重要,因为前者只是字节,而后者是字符串。

如何让 IronPython 也将第一行视为字符串?

4

2 回答 2

0

这看起来确实很奇怪,但在 python 2.7 中,常规字符串字节字符串。实际上,byte()是 的同义词str。如果你打印repr(bytes("hello"))你会看到'hello',不是b'hello'。在 python 3 中,这当然是另一回事了。

In short, you are getting a byte string in CPython already, so I believe there's no actual problem. (I'd certainly check if something is broken before spending more time trying to "fix" it). I don't use IronPython, but I have read that it uses unicode as the default type for strings (like Python 3). I'm concluding that b'string' in IronPython means the same thing as 'string' in CPython 2 (and 'string' means the same thing as CPython's u'string').

于 2012-04-12T14:07:15.670 回答
0

Short answer:

str(next(input))

Longer answer: It's pretty weird that the first in is bytes but the second is a string. It's probably just a bug. Can you please open an issue with a self-contained reproduction?

于 2012-04-12T15:13:03.260 回答