0

I have a text file like below

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

If I read a file and print it, it shows

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

However, If I do like below with the same string,

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


I want to read a text file like "1.txt" and print it like the below one. I can not find how to do it. Please help me. Thanks.

이동준
  • 21
  • 1
  • 2
  • `\u....` in *string literals* are being interpreted, read from text files they're not. – deceze Apr 09 '18 at 08:11
  • @deceze, Thanks for your comment. Is there a way to handle \u... in text file? – 이동준 Apr 09 '18 at 08:32
  • Seems to have been discussed already before. Please have a look at : https://stackoverflow.com/questions/2594810/removing-non-breaking-spaces-from-strings-using-python – Kenstars Apr 09 '18 at 08:18

1 Answers1

0

\u00a0 is a non break space and is one character.

In your first example your are reading \u00a0 as 6 chars.

If you want to read a file with \u00a0s and interpret them as spaces, you would have to parse the file yourself and create spaces for each \u00a0.

remi
  • 937
  • 3
  • 18
  • 45
  • Thank you for your answer. However, I have several more unicodes. \u20132, \u20136, \u00e4 etc. Should I parse every \u things to read it? Thanks. – 이동준 Apr 09 '18 at 08:27
  • If you look at the question in the answer of @Kenstars, one answer to that question is https://stackoverflow.com/a/2594942/4572356 . It is maybe the easiest way to do what you want to accomplish. Essentially using `.replace` on the string to replace a `\u...` with what it's definition is. – remi Apr 09 '18 at 08:32