0

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?

cs95
  • 379,657
  • 97
  • 704
  • 746
  • I suggest deleting this question and going back to the drawing board. This problem cannot be reproduced because it is caused by something upstream in your environment we don't have access to. I'm deleting my answer. – cs95 Dec 25 '18 at 18:54
  • I have been at the drawing board for about a week now. If I knew what environment variables were affecting my solution, I would have implemented them. I have set up different virtual environments, used a VM, and am still getting the same output. – Sumtingwong Dec 25 '18 at 19:56
  • Perhaps you can decode your string with the unicode_escape scheme. See the python3 example in this answer: https://stackoverflow.com/a/6868026/4909087 – cs95 Dec 25 '18 at 20:01
  • @coldspeed, thanks for your help here, much appreciated. Looks like `\f, \t,` etc. are interpreted as single characters, not form feeds, tabs, etc. I used `str.replace()` for these few and `str.translate()` for the punctuation. Would love to see how this could be implemented with `str.translate()`. – Sumtingwong Dec 25 '18 at 21:31
  • Keep in mind that your file is **just text**, not Python source code. If you open a text editor and type `\t` into your file, then it really does contain a backslash and a lowercase t - not a tab. – Karl Knechtel Aug 04 '22 at 23:38

0 Answers0