Python 类型错误:序列项 0:预期的 str 实例,找到的字节数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32071536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: sequence item 0: expected str instance, bytes found
提问by Muhammad Umer
for line in fo:
line = " ".join(line.split())
line = line.strip()
I am getting an error
我收到一个错误
line = ''.join(line.split())
TypeError: sequence item 0: expected str instance, bytes found
its working fine in python 2.x, but not working on 3.4 kindly suggest a proper solution for that
它在 python 2.x 中工作正常,但不能在 3.4 上工作,请为此提出适当的解决方案
回答by Kasramvd
' '
is a string which you're calling its join
method with a byte sequence. As the documentation's stated, in python-3.x:
' '
是一个字符串,您使用join
字节序列调用它的方法。正如文档所述,在 python-3.x 中:
str.join
Return a string which is the concatenation of the strings in the iterableiterable. A TypeErrorwill be raised if there are any non-string values in iterable, including bytesobjects. The separator between elements is the string providing this method.
str.join
返回一个字符串,它是可迭代的iterable 中字符串的串联。甲类型错误,如果有在可迭代任何非字符串的值,包括将提高字节对象。元素之间的分隔符是提供此方法的字符串。
But in this case since you are dealing with byte objects you cannot use str
related methods. The byte object itself comes with a join()
method that can be used in the same manner as str.join
. You can also use io.BytesIO
, or you can do in-place concatenation with a bytearray
object. As the documentation's mentioned bytearray
objects are mutable and have an efficient overallocation mechanism.
但是在这种情况下,由于您正在处理字节对象,因此不能使用str
相关方法。byte 对象本身带有一个join()
方法,可以以与str.join
. 您也可以使用io.BytesIO
,或者您可以与bytearray
对象进行就地连接。由于文档中提到的bytearray
对象是可变的,并且具有有效的过度分配机制。
So you can simply add a b
prefix to the empty string to make it a byte object:
因此,您可以简单地b
向空字符串添加前缀以使其成为字节对象:
line = b" ".join(line.split())
Also, if your file is contain strings you can simply open your file in a str
mode ('r'
)instead of byte ('rb'
).
此外,如果您的文件包含字符串,您可以简单地以str
模式 ( 'r'
) 而不是字节 ( 'rb'
)打开文件。
with open("input.txt", "r") as f:
# Do something with f
Note that despite the separation between str
and byte
objects in python-3.x, in python-2.x you only have str
. You can see this by checking the type of a string with b
prefix:
请注意,尽管在 python-3.x 中将str
和byte
对象分开,但在 python-2.x 中,您只有str
. 您可以通过检查带有b
前缀的字符串的类型来看到这一点:
In [2]: type(b'')
Out[2]: str
And that's what that makes the following snippet work:
这就是使以下代码段起作用的原因:
"".join([b'www', b'www'])
回答by user3126530
You could use str()
function:
你可以使用str()
功能:
lines=str(lines)
before running the command to avoid errors.
在运行命令之前避免错误。
This way you convert the lines
variable to string.
这样您就可以将lines
变量转换为字符串。
回答by Adnan Y
If you came here searching for a solution to join a custom class implemented in C/C++, the simplest method is to add a join method to the class itself and create binding to python.
如果你来这里是为了寻找加入一个用 C/C++ 实现的自定义类的解决方案,最简单的方法是向类本身添加一个 join 方法并创建到 python 的绑定。
For example, a class that can have either list or map which should be joinable, example code in pybind11 would be something like this:
例如,一个可以有列表或地图的类应该是可连接的,pybind11 中的示例代码将是这样的:
py::class_<Data> _data(m, "Data");
_data.def(py::init<>())
.def("join", [] (Data &d, const char *j = ' ') {
std::string ret;
if (d.isObject())
for (auto &o: d.object())
ret += o.first + j;
else if (d.isList())
for (auto &o: d.list())
ret += o.stringValue() + j;
return ret;
})
Then in python, it is a simple matter of calling the join method for the class
那么在python中,调用类的join方法就很简单了
data.join('_')