I am trying to use str.translate() to remove \t,\r, etc. from a line in an opened file. This works when typed into a Python 3 shell:
In [1]: def scrub_line(line):
...: import string
...: remap = {
...: ord('\t'): ' ',
...: ord('\f'): ' ',
...: ord('\n'): ' ',
...: ord('\r'): None,
...: }
...: return line.translate(remap)
In [2]: print(scrub_line("'pýtĥöñ\fIS\tawesome\r\n"))
'pýtĥöñ IS awesome
But when I do this, it does not work from the shell or a separate python file:
In [3]: for line in open("text.txt"):
...: print(scrub_line(line))
...:
'pýtĥöñ\fIS\tawesome\r\n
Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section The standard type\fhierarchy).
An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to \r right.
I inserted some random chars from the remap
table above in the second and third lines.
Noob here, what am I not getting?