python3.9 with open as ‘r‘ 模式下内容出现 _io.TextIOWrapper 解决方案

  • Post author:
  • Post category:python


python3.9 with open as ‘r’ 模式下内容出现 _io.TextIOWrapper 解决方案

在使用类似以下代码时

with open ( ‘textsave.txt’,’r’, encoding=’UTF-8′ ) as object_list_secondn:

object_list_secondn.read()

object_list_secondn=str(object_list_secondn)

print(object_list_secondn)

with open ( 'textsave.txt','r', encoding='UTF-8' ) as object_list_secondn:
        object_list_secondn.read()
        object_list_secondn=str(object_list_secondn)
        print(object_list_secondn)

打印结果为

<_io.TextIOWrapper name=’textsave.txt’ mode=’r’ encoding=’UTF-8′>

<_io.TextIOWrapper name='textsave.txt' mode='r' encoding='UTF-8'>

没有出现预想的打印结果

是因为object_list_secondn=文件信息

只需让object_list_secondn.read()  —> object_list_secondn=object_list_second.read()

变成

with open ( ‘textsave.txt’,’r’, encoding=’UTF-8′ ) as object_list_second:

object_list_secondn=object_list_second.read()

object_list_secondn=str(object_list_second)

print(object_list_second)

with open ( 'textsave.txt','r', encoding='UTF-8' ) as object_list_second:

        object_list_secondn=object_list_second.read()

        object_list_secondn=str(object_list_second)

        print(object_list_second)



版权声明:本文为qq_23949975原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。