Python 3-从文件中读取字符串并在代码中定义相同的字符串,但功能有所不同

2024-05-21 12:54:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本文件如下

    # 1.txt
    who is the\u00a0winners\u00a0where\u00a0season result\u00a0is 7th

如果我读了一个文件并打印出来,它就会显示出来

    >>> s = open("1.txt").read()
    >>> print(s)
    who is the\u00a0winners\u00a0where\u00a0season result\u00a0is 7th

但是,如果我喜欢下面的同一个字符串

    >> s = "who is the\u00a0winners\u00a0where\u00a0season result\u00a0is 7th"
    >> print(s)
    who is the winners where season result is 7th


我想读一个像“1.txt”这样的文本文件,然后像下面这样打印出来。我找不到怎么做。请帮帮我。谢谢。你知道吗


Tags: 文件the字符串txtreadisresultopen
1条回答
网友
1楼 · 发布于 2024-05-21 12:54:45

\u00a0是一个非中断空间,是一个字符。你知道吗

在第一个示例中,您将\u00a0读取为6个字符。你知道吗

如果要读取带有\u00a0的文件并将其解释为空格,则必须自己解析文件并为每个\u00a0创建空格。你知道吗

相关问题 更多 >