Cannot wrap sys.stdout in Jupiter Notebook with io.TextIOWrapper

Asked 2 years ago, Updated 2 years ago, 67 views

import sys,io

# Encoded to UTF8
sys.stdout=io.TextIOWrapper(sys.stdout, encoding='utf-8')

print ("hello")

After executing the above code,

UnsupportedOperation Traceback (most recent call last)
<ipython-input-4-7af1dd44 dff7>in<module>()
     21s.decompose()
     22 text = ''.join(soup.strapped_string)
--- > 23 print(text)

UnsupportedOperation:not writable

The error occurred.I don't know the cause, so please tell me.Running on jupyternotebook

python jupyter-notebook

2022-09-30 21:32

1 Answers

If you are using jupyter notebook, the output is ipython.

import sys,io
sys.stdout

Running the code above

<ipykernel.iostream.OutStream at 0xXXXXXXXX>

That's what it looks like.

If I were to use the output to jupyter notebook for code conversion, I think the code would look like the one below.

sys.stdout=ipykernel.iostream.OutStream(encoding='utf-8')

The following error occurs:

------------------------------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-90d17bbb039b>in<module>()
---->1sys.stdout=ipykernel.iostream.OutStream(encoding='utf-8')

TypeError: __init__()got an unexpected keyword argument 'encoding'

Now that the UTF-8 is fixed, you may forget to encode it into UTF8.


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.