Skip to content

Instantly share code, notes, and snippets.

@KatsuhiroMorishita
Created July 18, 2017 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatsuhiroMorishita/aa0869b8564f869dc56b57386f551fa4 to your computer and use it in GitHub Desktop.
Save KatsuhiroMorishita/aa0869b8564f869dc56b57386f551fa4 to your computer and use it in GitHub Desktop.
Pythonで扱えない文字の入っているUTF-8のファイルから、処理できる文字のみを残します。なお、副作用として、文字列が崩れる可能性があります。
# 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