'Is there a simpler way to hide a message in text?
I'm a newbie in Python. I have a sample.txt with random generated numbers, like:
19037221033587391092437079557028877692702595511401290586220309427782043275898955
13014634281984666840672700841593682750341042603005069133412414693645751888948174
56838541727973720849200844623086602683492491763746229651640999725476969534996546
12627011765843518895372932379406833789092525082734142624019628192647653889804387
As You see, my little program is replacing in a chosen place [char=1], in a selected [line=2], number with a letters from (input). Do you have any sugestions how to simplify it and make it more pythonic?
class MagicBox:
def __init__(self, q=0):
self.q = q
def oldtxt(self, line):
with open('sample.txt', 'r') as f:
for line in f.readlines()[line:line+1]:
return line
f.close()
def newtxt(self, line, char, newchar):
with open('sample.txt', 'r') as f:
for line in f.readlines()[line:line + 1]:
x = (list(line))
x[char] = newchar
return ''.join(x)
f.close()
def replace(self, file, old, new):
for line in fileinput.input(file, inplace=1):
line = line.replace(old, new)
sys.stdout.write(line)
def change(self, input, n, line, char, newchar):
old_txt = oldtxt(line)
new_txt = newtxt(line, char, newchar)
for linia in fileinput.input("sample.txt", inplace=1):
linia = linia.replace(old_txt, new_txt)
sys.stdout.write(linia)
def doIT(self, input, line=0, char=0, step=0):
n = 0
d = list(input)
for t in range(1, len(input)+1):
self.change(line, char, d[n])
char += step
n += 1
doIT("Big Brown Fox", 0, 5, 7)
Solution 1:[1]
The following solution might be a bit complicated, but I'll explain how it works and you should be able to replace some of it with your own, more understandable code.
def change(data, word, step, line, start_index):
data = [[char for char in line] for line in data.split("\n")]
for index in range(start_index, len(word) * step, step):
data[line][index] = word[index // step]
return "\n".join(["".join(line) for line in data])
new_data = change(old_data, "MAGIC", 3, 2, 1)
print(new_data)
The first line takes the data
variable, which is a string, and splits it up into a 2D array, in this case, an array of lines in the data which are arrays of the characters in that line (sorry if that doesn't make sense). If you're unfamiliar with single-line for loops, you could also split the data like so:
data = data.split("\n")
for i in range(len(data)):
data[i] = list(data[i])
Next, we make a loop that iterates through the indices that each letter in word
need to inserted in data
. If you were to print the range as a list, for this example it would look like [1, 4, 7, 10, 13]
.
Then for each of those indices, we get the exact character in the list on the specified line, and change it to the corresponding letter in "MAGIC"
.
Finally, the 2D array that has been edited, gets joined together again to become a string. And again, you can rewrite that similarly to how the first line can be rewritten, but reversed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |