Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am looking for a string that will crash python3 when encoding it to UTF8.
s=?
s.encode("UTF-8") -> results in error
Thank you for your help
The UTF-8 encoder does not allow Unicode strings to use the the UTF-16 "surrogate" code points from U+D800 to U+DFFF:
>>> s = '\ud800'
>>> s.encode('UTF-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud800' in position 0: surrogates not allowed
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Trying to decode bytes
2k2P3PKIfViQ1L6TTc7kYks6bpeat6pPH9qRrNcj1S2195TYz\x88}\x88\x88JKgqzeXz96zKrTX05D9bkJf1yCf
Is there a way to convert \x88 to letter or hide it.
trying this
s = b'2k2P3PKIfViQ1L6TTc7kYks6bpeat6pPH9qRrNcj1S2195TYz\x88}\x88\x88JKgqzeXz96zKrTX05D9bkJf1yCf'
d = s.decode('utf-8')
but got error
*** UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 64: invalid start byte
Any Help?? Thanks in advance...
Why do you think it's UTF-8? UTF-8 is a specific, self-checking encoding, you can't just decode random bytes with it. If you just want to convert every byte to the equivalent Unicode ordinal, decode with latin-1. Decoding with cp1252 will even make a useful printable character. Or choose any other one byte per character ASCII encoding and see what it looks like. With no idea what it's supposed to mean, any 1-1 bytes to text encoding works, it's the logic of your program that determines if it's correct.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
class result:
def _init_(self,phy,chem,math):
self.phy=phy
self.chem=chem
self.math=math
def printavg(self):
print(f"average={(self.phy+self.chem+self.math)/3}")
rollone=result(86,95,85)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: result() takes no arguments
getting the same error again and I used init constructor in the right way
is anyone to explain is most welcome and benevolent of you?
Your declaration of init is wrong, it is __init__ and not _init_, ie, with double underscores :
class result:
def __init__(self,phy,chem,math):
self.phy=phy
self.chem=chem
self.math=math
def printavg(self):
print(f"average={(self.phy+self.chem+self.math)/3}")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a set of binary number, some of them start with 0, for example:
000000000001
When I use hex(000000000001) I get the following error:
>>> hex(0000000000000001)
File "<stdin>", line 1
hex(0000000000000001)
^
SyntaxError: invalid token
But I dont have with :
>>> hex(0000000000000000)
'0x0'
How to pass some digit if they start with zero?
You have to do the following steps:
string_of_bits = '0b' + '000000000001'
hex(int(string_of_bits,2))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Alright I want to turn a binary string like '1010110000000010' into a bytes object in Python 3.3.2. Anyone know how I can do this? Thanks!
If you want b'1010110000000010', use bytes or str.encode:
>>> bytes('1010110000000010', encoding='ascii')
b'1010110000000010'
>>> '1010110000000010'.encode('ascii')
b'1010110000000010'
>>> '1010110000000010'.encode() # can omit encoding (default: utf-8)
b'1010110000000010'
If you want b'\xac\x02', use bytes with converted integers (using int with base 2), or struct.pack:
>>> bytes(int(b[i:i+8], 2) for i in range(0, len(b), 8))
b'\xac\x02'
>>> import struct
>>> b = '1010110000000010'
>>> b''.join(struct.pack('B', int(b[i:i+8], 2)) for i in range(0, len(b), 8))
b'\xac\x02'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
The code I'm using is:
import time
print(time.strftime("%H:%M:%S"))
In the dynamic shell, this (as you would expect) outputs a formatted string, e.g. 03:21:35
When executing the exact same code from a file, it throws the following error:
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(time.strfime("%H:%M:%S"))
AttributeError: 'module' object has no attribute 'strfime'
Anybody got any idea as to why this might be happening, and more importantly, how to fix it?
You've got a typo:
print(time.strfime("%H:%M:%S"))
should be
print(time.strftime("%H:%M:%S"))