Skip to content
Advertisement

Python reading from file vs directly assigning literal

I asked a Python question minutes ago about how Python’s newline work only to have it closed because of another question that’s not even similar or have Python associated with it.

I have text with a ‘n’ character and ‘t’ in it, in a file. I read it using

open().read()

I then Stored the result in an identifier. My expectations is that such a text e.g

InlovetCoding

being read from a file and assigned to an identifier should be same as one directly assigned to the string literal

"InlovetCoding"

being directly assigned to a file.

My assumption was wrong anyway

word = InlovetCoding

ends up being different from

word = open(*.txt).read()

Where the content of *.txt is exactly same as string “InlovetCoding”

Edit:

I did make typo anyway, I meant t && n , searching with re module’s search() for t, it return None, but t is there. Why is this please?

Advertisement

Answer

You need to differentiate between newlines/tabs and their corresponding escape sequences:

for filename in ('test1.txt', 'test2.txt'):
    print(f"n{filename} contains:")
    fileData = open(filename, 'r').read()
    print(fileData)

    for pattern in (r'\n', r'n'):
        # first is the escape sequences, second the (real) newline!
        m = re.search(pattern, fileData)
        if m:
            print(f"found {pattern}")

Out:

test1.txt contains:
InlovetCoding
found \n

test2.txt contains:
I
love    Coding
found n
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement