Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

python - TypeError: sequence item 0: expected str instance, bytes found

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

question from:https://stackoverflow.com/questions/32071536/typeerror-sequence-item-0-expected-str-instance-bytes-found

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

' ' is a string which you're calling its join method with a byte sequence. As the documentation's stated, in python-3.x:

str.joinReturn a string which is the concatenation of the strings in the iterable iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.

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.

So you can simply add a b prefix to the empty string to make it a byte object:

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').

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:

In [2]: type(b'')
Out[2]: str

And that's what that makes the following snippet work:

"".join([b'www', b'www'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...