#🔒 codec cant decode even with correct encoding
129 messages · Page 1 of 1 (latest)
@naive sand
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!paste
heres the code https://paste.pythondiscord.com/DJQA
i have almost no idea what any of these mean, im just messing around and encountered this
why not just use UTF-8 instead of what ever the system reports as the locally preferred encoding?
because
uh
idk how to do that
the patch wasnt even made by me, it was made by a friend and idk how it works
The answer is to probably just use utf-8 but there is also a standard library encoding detector that may work depending on the output
!d tokenize.detect_encoding
tokenize.detect_encoding(readline)```
The [`detect_encoding()`](https://docs.python.org/3/library/tokenize.html#tokenize.detect_encoding) function is used to detect the encoding that should be used to decode a Python source file. It requires one argument, readline, in the same way as the [`tokenize()`](https://docs.python.org/3/library/tokenize.html#tokenize.tokenize) generator.
It will call readline a maximum of twice, and return the encoding used (as a string) and a list of any lines (not decoded from bytes) it has read in.
It detects the encoding from the presence of a UTF-8 BOM or an encoding cookie as specified in [**PEP 263**](https://peps.python.org/pep-0263/). If both a BOM and a cookie are present, but disagree, a [`SyntaxError`](https://docs.python.org/3/library/exceptions.html#SyntaxError) will be raised. Note that if the BOM is found, `'utf-8-sig'` will be returned as an encoding...
skip the line
encod = locale.getpreferredencoding(False)
```and change another line to
```py
output = output_bytes.decode("utf-8")
its throwing the error invalid start byte
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 185: invalid start byte
this at least works on my linux system (which is set to natively use utf-8 out of the box)
subprocess.check_output(["ifconfig"], encoding="utf-8")
```(as it's linux i changed `ipconfig` to `ifconfig`)
shell=True is the default for subprocess.check_output() anyways
cp1253 ("Greek - ANSI") is one of the different character encodings that windows use depending on your language/locale settings
I kinda want a problematic excerpt of the logfile... is it seriously an exotic text encoding?
Unless your text is ASCII or utf-8, something is weird.
so neither
import subprocess
output = subprocess.check_output(["ipconfig"], encoding="utf-8")
with open("logfile.txt", "w") as file:
file.write(output)
```or
```py
import subprocess
output = subprocess.check_output(["ipconfig"], encoding="cp1253")
with open("logfile.txt", "w") as file:
file.write(output)
```works?
pretty sure they didn't when i last checked
Show us a problematic piece of the logfile, is what I mean
let me check again
the logfile is empty
or you could try the hacky way:
import subprocess
output = subprocess.check_output(["ipconfig"])
with open("logfile.txt", "wb") as file:
file.write(output)
```and you'll have the raw output bytes in the log file instead
due to the encoding step failing and the script aborts before it can be written to the file
can i decode them later on?
Well, by extension I mean what you're trying to write to the file...
Windows also likes to produce ansi/mbcs text. You should re-encode it if you get that stuff, it sucks.
in that last case you would have the raw output in the file in whatever encoding it was in, hopefully windows can recognize it when you open the file or you can set the encoding that it should be read as in for example notepad
only this works
some characters have a rhombus with a question mark
it should work, but the log file content might look strange if you open the file and try to decode it using the wrong encoding
and there's some random chinese letters too
yeah, that is probably due to that problem
Try viewing the log file using mbcs/ansi encoding.
It came from windows, so windows probably used their ugly encoding.
also how did my ipv4 address change shouldn't it be the same?
alright
it still won't work
maybe time to try out @midnight thunder suggestion above?
it's fine tbh, as long as i can still read the stuff written in English and the numbers
thanks for the help!
I meant write raw bytes to the file and use your text editor to view it as ANSI.
the only corrupted characters are the greek ones which idrc about
it defaults as ansi
Hmmm... alright. Maybe it really is windows trynna be smart with some weird encoding like 1253. Glad you got it working well enough tho
thanks! and sorry for the hassle
i must up my knowledge about encodings
i didn't even know the output text is encoded when i started out the script
can you provide a sample (as text, not a screenshot) as it is output by print(output) on the screen? (you can change your ip address and mac address if you want to anonymize it)
!code
one second
because then we will see the bytes, and non-printable bytes and such will be encoded as hex or similar
b'\r\nWindows IP Configuration\r\n\r\n\r\nEthernet adapter Ethernet:\r\n\r\n Media State . . . . . . . . . . . : Media disconnected\r\n Connection-specific DNS Suffix . : \r\n\r\nWireless LAN adapter \x92\xa6\xa7\xa0\xa1\xe3 \xa9\xe7\xa4\x9b\x9c\xa9\x9e* 1:\r\n\r\n Media State . . . . . . . . . . . : Media disconnected\r\n Connection-specific DNS Suffix . : \r\n\r\nWireless LAN adapter \x92\xa6\xa7\xa0\xa1\xe3 \xa9\xe7\xa4\x9b\x9c\xa9\x9e* 2:\r\n\r\n Media State . . . . . . . . . . . : Media disconnected\r\n Connection-specific DNS Suffix . : \r\n\r\nMobile Broadband adapter \x83\xe5\xa1\xab\xac\xa6 \xa1\xa0\xa4\x9e\xab\xe3\xaa \xab\x9e\xa2\x9c\xad\xe0\xa4\xe5\x98\xaa:\r\n\r\n Media State . . . . . . . . . . . : Media disconnected\r\n Connection-specific DNS Suffix . : \r\n\r\nWireless LAN adapter Wi-Fi:\r\n\r\n Connection-specific DNS Suffix . : \r\n Link-local IPv6 Address . . . . . : (ipv6) IPv4 Address. . . . . . . . . . . : (ipv4) Subnet Mask . . . . . . . . . . . : (idek what this is) Default Gateway . . . . . . . . . :(this neither)'
if i missed any sensitive info pls dont dox me
can you try
import subprocess
output = subprocess.check_output(["ipconfig"], encoding="cp737)
with open("logfile.txt", "w") as file:
file.write(output)
```and see if the content makes sense to you after that? (i don't know greek)
if it even works and not errors out on you again
i tried writing bytes and then decoding with 737 but it only shows the unknown character symbol instead of the mixed chinese
you might also try iso8859-7
instead of cp737?
yeah
but for me it just errored out for the sample you sent
yeah, but cp737 at least doesn't throw the error
yea
i need to go afk for a while
idk if i understood correctly but chcp and the locale command give out different encodings even tho they should be the same?
kind of a long-shot that seems to be windows specific as i can only use it with python on windows and not on linux
if you set the encoding to either oem or mbcs, for example:
import subprocess
output = subprocess.check_output(["ipconfig"], encoding="oem")
with open("logfile.txt", "w") as file:
file.write(output)
see if it gives you anything readable and without throwing a exception at you 🤞
so windows just sucks?
on it
nope, still the same, unknown characters instead of greek letters
well, microsoft does things their own way and historically hasn't been much for following standards (that they haven't invented them selves) and even kept a lot of stuff proprietary (but that has been changing but they still have a lot of legacy to drag around), but it's mostly just "different" 😉
and you did change the mode in the open to be "w" and not "wb" when you ran this?
as when we are trying to decode we shouldn't try to write it as raw bytes to the file
yes
this is really hard as i can't even see if it's greek characters or just some strange letters as i can't read greek at all
can you try to print it out to the terminal instead of a file?
I'm pretty sure we did that before too
unless you mean with oem encoding
import subprocess
prnt(isubprocess.check_output(["ipconfig"], encoding="oem"))
and try some different encodings besides "oem" as well
printing does work
such as "mbcs" or "cp737" as well
mbcs doesn't work
okay, so only "oem" works?
oem still doesn't write the greek letters in the file
it only decodes them to print them
no, but at least the to terminal, right?
yea
what if we try:
import subprocess
output = subprocess.check_output(["ipconfig"], encoding="oem")
with open("logfile.txt", "w", encoding="utf-8") as file:
file.write(output)
```and you them make sure that the application that you are reading the log file with is set to "auto" or UTF-8 ?
trying to force it to make the right thing happen
bruh
it works
vs code decided to just not read it
notepad does tho
thank you SO much!
might be some setting in vs code that needs to be adjusted 🤷
possibly
you're welcome and good luck with your project
thanks!
you might want to delete the sample output
just so that it doesn't stick around here in the read-only archives after this thread has been closed in-case the greek text that i can't read (but someone else might) contains anything sensitive
no it's just translation of the English text
when you are done with the help thread you may close it with !close or you can wait for it to be closed by the bot after an hour without any new messages in the thread (as edits and reactions don't count towards the idle timeout)
👋
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.