0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

変数名を一括で変換するプログラム

Last updated at Posted at 2020-12-12

変数名を一発で変換したい

環境によってはηやδが使えないときってありますよね。
そんなときにtxtで保存したソースコードの指定した一文字の変数を変換してくれるヤツです。

replace.py
with open('source_code.txt', mode='rt', encoding='utf-8') as f:
    data = list(f)

target = ""
replace = ""

replaced = []
for i in range(len(data)):
    if target in str(data[i]):
        replaced.append(str(data[i]).replace(target,replace))
    else:
        replaced.append(str(data[i]))
replaced = ''.join(replaced)

with open("replaced.txt",mode = "w",encoding='utf-8') as f:
    f.write(replaced)

(追記)もっと簡潔に書けました。

ファイルから読みだしたデータは文字列なのでforループで回す必要無かったです。
@shiracamus 様ありがとうございます。

replace.py
with open('source_code.txt', encoding='utf-8') as f:
    text = f.read()

target = ""
replace = ""
replaced = text.replace(target, replace)

with open("replaced.txt", "w", encoding='utf-8') as f:
    f.write(replaced)

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?