Created
July 18, 2017 12:09
-
-
Save KatsuhiroMorishita/aa0869b8564f869dc56b57386f551fa4 to your computer and use it in GitHub Desktop.
Pythonで扱えない文字の入っているUTF-8のファイルから、処理できる文字のみを残します。なお、副作用として、文字列が崩れる可能性があります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# purpose: Pythonで扱えない文字の入っているUTF-8のファイルから、処理できる文字のみを残す | |
# memo: 副作用として、文字列が崩れる可能性もある。 | |
# author: Katsuhiro Morishita | |
# created: 2017-07-18 | |
# license: MIT | |
import sys | |
import os | |
def main(): | |
# 引数から処理対象のファイル名を取得 | |
argvs = sys.argv # コマンドライン引数を格納したリストの取得 | |
if len(argvs) < 2: | |
print("引数で処理対象を指定して下さい") | |
exit() | |
target = argvs[1] # 分割対象のファイル名を取得 | |
# 指定されたファイルを処理 | |
name, ext = os.path.splitext(target) | |
txt = open(target, "r", encoding="utf-8-sig", errors="ignore").read() | |
open("{0}_copy{1}".format(name, ext), "w", encoding="utf-8-sig", errors="ignore").write(txt) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment