Austin Smith Austin Smith - 1 year ago 77
Python Question

Replacing unknown characters in a file

Let's say I have a file 'test.txt' and in it is huge amounts of text.

Now I am going to encode all of these characters into a custom binary/hex I wrote, but I can only use character I have pre-defined.

My question is, how can I define characters(in a LIST or a STRING), and have any other characters replaced with.. lets say a '?'


I have "abcdefg#~" in 'test.txt' file.
And I have the string:

char = '.,/?~abcdefg' #legal characters

Without knowing that '#' is in there, and without specifically looking for '#'
how can I replace all characters that are not listed in 'char' with a '?'

P.S I will not be reading from the file directly with this. I should of stated this before, but I will be saving the file contents to a string first for farther editing, and then save the finished edit to a separate file post-conversion.

Answer Source

Put the legal characters in a set then use in to test each character of the string. Construct the new string using the str.join() method and a conditional expression.

>>> s = "test.,/?~abcdefgh"
>>> legal = set('.,/?~abcdefg')
>>> s = ''.join(char if char in legal else '?' for char in s)
>>> s
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download