2016-06-27 9 views
1

特定のキーワードを除くすべてのコンテンツがぎこちなく置き換えられるようにファイルを匿名化しようとしていますが、フォーマットは同じです(句読点、長さ文字列と大文字の区別)。例えば:私はPythonでこれを実行しようとしていますが、私は解決策を見つけるには運がないよファイルを繰り返して文字列を置き換えて文字数をそのまま残す

T ad ehistmg ptrs, erovj qo giw! Tgds ar o qpyeogf: long 
Yeg, rmbjthe yadn. 

I am testing this, check it out! This is a keyword: long 
Wow, another line. 

がに変わります。私は、トークン化と別のファイルへの書き込みを使って置き換えようとしましたが、大した成功はありませんでした。

+2

あなたが試したことがあるコードを投稿できますか? – Andrew

+1

何を試してみましたか、何が困難でしたか? – alecxe

+0

あなたは「匿名」と言っています。法律上の義務がある場合は、特にスクラブされたデータを回復することが不可能であることを確認する必要がある場合は、無作為にインターネットの見知らぬ人に頼るのではなく、あなたがするよりも手がかりのほうが多い。 – user2357112

答えて

1

最初にいくつかのキーワードを保存しなければならないという事実を無視しましょう。後でそれを修正します。

この種の1対1マッピングを実行する最も簡単な方法は、方法str.translateを使用することです。 stringモジュールには、すべてのASCII小文字と大文字を含む定数も含まれており、ランダム置換を得るためにrandom.shuffleを使用できます。パイソン2において

import string 
import random 

random_caps = list(string.ascii_uppercase) 
random_lows = list(string.ascii_lowercase) 

random.shuffle(random_caps) 
random.shuffle(random_lows) 

all_random_chars = ''.join(random_lows + random_caps) 

translation_table = str.maketrans(string.ascii_letters, all_random_chars) 

with open('the-file-i-want.txt', 'r') as f: 
    contents = f.read() 
    translated_contents = contents.translate(translation_table) 

with open('the-file-i-want.txt', 'w') as f: 
    f.write(translated_contents) 

str.maketrans代わりstrの静的メソッドのstringモジュール内の関数です。

translation_tableは、文字から文字へのマッピングであるため、すべての単一のASCII文字を他の文字にマッピングします。 translateメソッドは、このテーブルを文字列の各文字に適用するだけです。

重要な注意:各文字は、そのユニークな他の文字にマッピングされたので、上記の方法は、実際に可逆です。これは、シンボルの周波数を簡単に分析することで、シンボルの反転が可能であることを意味します。

あなたは、これが困難または不可能にしたい場合は、すべての行のtranslation_tableを再作成することができます。

import string 
import random 

random_caps = list(string.ascii_uppercase) 
random_lows = list(string.ascii_lowercase) 

with open('the-file-i-want.txt', 'r') as f: 
    translated_lines = [] 
    for line in f: 
     random.shuffle(random_lows) 
     random.shuffle(random_caps) 
     all_random_chars = ''.join(random_lows + random_caps) 

     translation_table = str.maketrans(string.ascii_letters, all_random_chars) 
     translated_lines.append(line.translate(translation_table)) 

with open('the-file-i-want.txt', 'w') as f: 
    f.writelines(translated_lines) 

はまた、あなたが行毎にファイルを変換し、救うことができることに注意してください:

with open('the-file-i-want.txt', 'r') as f, open('output.txt', 'w') as o: 
    for line in f: 
     random.shuffle(random_lows) 
     random.shuffle(random_caps) 
     all_random_chars = ''.join(random_lows + random_caps) 

     translation_table = str.maketrans(string.ascii_letters, all_random_chars) 
     o.write(line.translate(translation_table)) 

これは、コード自体が致命的でない限り、このコードで膨大なファイルを翻訳できることを意味します。


アカウントなどのキーワードを考慮せずに、すべて文字をいじり上記のコード。 (キーワードをprevservesバージョンを使用して)

import re 
import string 
import random 

random_caps = list(string.ascii_uppercase) 
random_lows = list(string.ascii_lowercase) 

keywords = ['long'] # add all the possible keywords in this list 

keyword_regex = re.compile('|'.join(re.escape(word) for word in keywords)) 


with open('the-file-i-want.txt', 'r') as f, open('output.txt', 'w') as o: 
    for line in f: 
     random.shuffle(random_lows) 
     random.shuffle(random_caps) 
     all_random_chars = ''.join(random_lows + random_caps) 

     translation_table = str.maketrans(string.ascii_letters, all_random_chars) 
     matches = keyword_regex.finditer(line) 
     translated_line = list(line.translate(translation_table)) 

     for match in matches: 
      translated_line[match.start():match.end()] = match.group() 

     o.write(''.join(translated_line)) 

使用例を:

要求を処理するための最も簡単な方法は、キーワードのいずれかが発生するかどうかを簡単に行ごとにチェックして、そこに「再挿入」することです:

$ echo 'I am testing this, check it out! This is a keyword: long 
Wow, another line.' > the-file-i-want.txt 
$ python3 trans.py 
$ cat output.txt 
M vy hoahitc hfia, ufoum ih pzh! Hfia ia v modjpel: long 
Ltj, fstkwzb hdsz. 

longが保存されていることに注意してください。

+0

@DavidCullen簡単な修正が追加されました。 – Bakuriu

+0

良い解決策。私はキーワードの保存に立ち往生し、そのような2回目のパスを使用することを考えていませんでした。 – jimo337

+0

@ jimo337私はそれが最も簡単な解決策だと思うし、それはまた非常に効率的です。最終的には、正規表現でこれらのキーワードを見つけるのは難しいでしょうから、他の唯一の選択肢は、まずキーワードを見つけ、それからキーワードではない部分を「翻訳」することですが、これはもっと面倒ですまた、パフォーマンスの大幅な変化を招くのではないかと疑います。 – Bakuriu

関連する問題