1

I have the following python-regex read.py file modified below from this question. In the same directory I have another text file called 1.txt.

read.py

import re
with open ("1.txt", "r") as text:
    data = text.read()
print( re.sub(r"(.)\1*", lambda x: "{}{}".format( x.group(1), len(x.group())),  text) )

1.txt

UUBBBRRLLLBBBDDD

I am trying to read the contents of 1.txt into text in the read.py structure and then get the same result as if I had inserted the text into the read2.py code (see below).

read2.py

import re
text = "UUBBBRRLLLBBBDDD"
print( re.sub(r"(.)\1*", lambda x: "{}{}".format( x.group(1), len(x.group())),  text) )

But if I run read.py I always get the following error:

Traceback (most recent call last):
  File "axios.py", line 5, in <module>
    print( re.sub(r"(.)\1*", lambda x: "{}{}".format( x.group(1), len(x.group())),  text) )
  File "/usr/lib/python3.8/re.py", line 208, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

I understand that I should read 1.txt as a variable and then read it in the excerpt print( re.sub(r"(.)\1*", lambda x: "{}{}".format( x.group(1), len(x.group())), text) ) from read.py.

But I don't understand why I didn't read it and it was resulting in an error and I can't read the file directly from read.py and get the same result in read1.py. I already googled it and haven't found anything involving module regex, read and print together in the same python file as they should work in read.py.

7beggars_nnnnm
  • 697
  • 3
  • 12
  • 1
    I'm voting to close this as typo/can't be reproduced, since the issue was seemingly just caused by using the wrong variable. – AMC Jan 04 '20 at 23:12
  • @AMC I still have no experience to be sure if this topic is useful or not open, but I trust the decision of experienced users if it is decided to close this issue.ThanKX! – 7beggars_nnnnm Jan 04 '20 at 23:16

1 Answers1

1

Shouldn't the "text" variable just be "data"? This works: print(re.sub(r"(.)\1*", lambda x: "{}{}".format(len(x.group()), x.group(1)), data))

"text" is a <class '_io.TextIOWrapper'> (i.e. a file-like object) that is read from and then closed, whereas "data" is the variable that holds a reference to a string.

Mr.C
  • 152
  • 7
  • I had tried this approach and it also worked but I am getting the numeric character "1" at the end of the line of the result. What is the reason? – 7beggars_nnnnm Jan 04 '20 at 20:54
  • 1
    Here is the output I get: `U2B3R2L3B3D3`. Could you post your exact output? – Mr.C Jan 04 '20 at 21:00
  • 1
    I discovered the cause (though I don't understand) and the cause was that the text in the 1.txt file was surrounded by double quotes at the end and beginning of the string.... – 7beggars_nnnnm Jan 04 '20 at 21:36
  • 1
    For `U2B3R2L3B3D3`: there are two counts of "U", followed by 3 counts of "B", etc. For `"1U2B3R2L3B3D3"1`: there is one count of the character `"`, followed by 2 counts of "B", etc. At the end, there is one more count of the character `"` – Mr.C Jan 04 '20 at 21:51
  • 1
    Exactly this was the output kkk, I forgot to post the output, forgive me. ThankX @Mr.C, I understood your explanation. – 7beggars_nnnnm Jan 04 '20 at 21:53