Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56280

Re: Odd-length string

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Odd-length string
Date 2013-10-06 15:50 -0400
References <0f1b6cd6-3025-4bdc-8f80-73a51a7ed241@googlegroups.com> <dc7e43a4-2f40-4275-8754-7e947f886fd9@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.794.1381089050.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 10/6/2013 10:37 AM, markotaht@gmail.com wrote:

> NEw problem is : Traceback (most recent call last):
>    File "C:\Users\Marko\Desktop\hacker.org\XOR cypher.py", line 35, in <module>
>      print("Key-" + str(võti) + ": " + str("".join(tulemus2)))
> TypeError: sequence item 0: expected str instance, bytes found

If ''.join(iterable_of_strings) executes, the result is a string and str 
would not be needed. If you try
"".join(tulemus2)
by itself, you will see the same error.

> If i  take away the join command i get this: Key-00000000: [b'u', b'o', b'\x00', b'\x1d', b' ', b'|', b'N', b'\x0f', b'9', b'j', b'K', b'J', b'&', b'#', b'A', b'K', b'5', b'k', b'_', b'\x1e', b',', b'j', b'\x0c', b'\x08', b'i', b'(', b'\x06', b'\\', b'r', b'3', b'\x1f', b'V', b's', b'9', b'\x1d']

Since tulemus2 is an iterable_of_bytes, you must join with bytes b'' and 
call str on the result. So just add the b prefix.

What Roy tried to say to you earlier is that when you get an error in a 
expression, and you do not understand *exactly* where in the expression 
the error is, pull the expression apart and test the pieces 
individually. This sometimes includes splitting

print(expression)  # into

s=expression
print(s)

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Odd-length string markotaht@gmail.com - 2013-10-06 06:10 -0700
  Re: Odd-length string Roy Smith <roy@panix.com> - 2013-10-06 09:46 -0400
  Re: Odd-length string Peter Otten <__peter__@web.de> - 2013-10-06 16:31 +0200
  Re: Odd-length string markotaht@gmail.com - 2013-10-06 07:37 -0700
    Re: Odd-length string Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-06 15:56 +0100
    Re: Odd-length string Terry Reedy <tjreedy@udel.edu> - 2013-10-06 15:50 -0400
    Re: Odd-length string Piet van Oostrum <piet@vanoostrum.org> - 2013-10-06 20:59 -0400

csiph-web