File1 File2
----- -----
abc abc
def
ghi def
abc
「 ↑2つのファイルを見比べて、含まれていない情報を 抽出するアプリが欲しい☆ 行単位でいい☆」
Set1 Set2
----- -----
abc abc
def
ghi def
「 ↑Set2 を正とするなら、Set2 を foreach で回して、Set1 の要素をリムーブしろだぜ☆」
Set1 Set2
----- -----
abc
ghi def
diff1-100/ex1/diff.py
print("Hello!")
Output:
Hello!
Python: コマンドライン引数とは?(超基礎)
【Python入門】foreachをPythonで使ってみよう
import sys
args = sys.argv
print("Say, {}!".format(len(args)))
for arg in args:
print("Hello, {}!".format(args))
Output:
Say, 1!
Hello, ['c:\\Users\\むずでょ\\OneDrive\\ドキュメント\\practice-py\\diff1-100\\ex1\\diff1.py']!
「 Visual Studio Code で コマンドライン引数を渡すには どうやればいいんだぜ☆?」
Output:
Say, 4!
Hello, ['c:\\Users\\むずでょ\\OneDrive\\ドキュメント\\practice-py\\diff1-100\\ex1\\diff1.py', 'apple', 'banana', 'cherry']!
Hello, ['c:\\Users\\むずでょ\\OneDrive\\ドキュメント\\practice-py\\diff1-100\\ex1\\diff1.py', 'apple', 'banana', 'cherry']!
Hello, ['c:\\Users\\むずでょ\\OneDrive\\ドキュメント\\practice-py\\diff1-100\\ex1\\diff1.py', 'apple', 'banana', 'cherry']!
Hello, ['c:\\Users\\むずでょ\\OneDrive\\ドキュメント\\practice-py\\diff1-100\\ex1\\diff1.py', 'apple', 'banana', 'cherry']!
import sys
args = sys.argv
print("Say, {}!".format(len(args)))
for arg in args:
print("Hello, {}!".format(arg)) # args --> arg
Output:
Say, 4!
Hello, c:\Users\むずでょ\OneDrive\ドキュメント\practice-py\diff1-100\ex1\diff1.py!
Hello, apple!
Hello, banana!
Hello, cherry!
「 これで コマンドライン引数で ファイルパスを渡せるな☆」
deff1-100/ex2/sample1.txt:
Ant
Bird
Dog
Eagle
deff1-100/ex2/sample2.txt:
Ant
Bird
Cat
Dog
Eagle
diff1-100/ex2/dff.py:
"""
Command line arguments:
"args": [
"C:/Users/むずでょ/OneDrive/ドキュメント/practice-py/diff1-100/ex2/sample1.txt",
"C:/Users/むずでょ/OneDrive/ドキュメント/practice-py/diff1-100/ex2/sample2.txt"
]
"""
import sys
args = sys.argv
# ファイル読取
file1 = open(args[1], "r")
file2 = open(args[2], "r")
# 行ごとにします
lines1 = file1.readlines()
lines2 = file2.readlines()
# 一行ずつ表示します
for line1 in lines1:
print(line1)
for line2 in lines2:
print(line2)
# ファイルをクローズします
file1.close()
file2.close()
Output:
Ant
Bird
Dog
Eagle
Ant
Bird
Cat
Dog
Eagle
「 プログラム実行中のメモリ上を調べなければ 意味なくないか☆?」
Pythonのreadline()は改行コードも含まれてしまう
import os
「 ↑ ファイルの冒頭で os モジュールをインポートして……☆」
line1 = line1.rstrip(os.linesep)
「 ↑ そのOSの改行コードを右端から削除すればいいらしい☆」
Output:
Ant
Bird
Dog
Eagle
Ant
Bird
Cat
Dog
Eagle
「 Python でカレントディレクトリの移動って どうやるんだぜ☆?」
import os
print("Info | Current directory: {}".format(os.getcwd()))
os.chdir(os.path.dirname(os.path.abspath(__file__)))
print("Info | Current directory: {}".format(os.getcwd()))
「 このファイルが置いてあるディレクトリーを カレント にするように変更しようぜ☆?」
「 ワーキング ディレクトリを別に作って、そこをカレント・ディレクトリにすればいいのに……☆
サンプル・プログラム番号ごとに カレント・ディレクトリを移動するということは
コピーするファイルが増えるだろうに……☆」
"""
Command line arguments:
"args": [
"./sample1.txt",
"./sample2.txt"
]
"""
「 ↑ 相対パスが使えれば、サンプル・プログラムごとにディレクトリ名を変えなくていいぜ☆」
# セット作成
set1 = set()
set2 = set()
# 一行ずつセットに追加
for line1 in lines1:
line1 = line1.rstrip(os.linesep)
set1.add(line1)
for line2 in lines2:
line2 = line2.rstrip(os.linesep)
set2.add(line2)
# 一要素ずつ表示します
for element1 in set1:
element1 = element1.rstrip(os.linesep)
print("Set1: {}".format(element1))
for element2 in set2:
element2 = element2.rstrip(os.linesep)
print("Set2: {}".format(element2))
Output:
Set1: Eagle
Set1: Ant
Set1: Bird
Set1: Dog
Set2: Ant
Set2: Eagle
Set2: Bird
Set2: Dog
Set2: Cat
「 ↑ いけてそうだな☆ セットは順序を覚えていないのでこれでいいぜ☆」
diff1-100/ex4/diff.py:
"""
Command line arguments:
"args": [
"./sample1.txt",
"./sample2.txt"
]
"""
import sys
import os
print("Info | Current directory: {}".format(os.getcwd()))
os.chdir(os.path.dirname(os.path.abspath(__file__)))
print("Info | Current directory: {}".format(os.getcwd()))
args = sys.argv
# ファイル読取
file1 = open(args[1], "r")
file2 = open(args[2], "r")
# 行ごとにします
lines1 = file1.readlines()
lines2 = file2.readlines()
# セット作成
set1 = set()
set2 = set()
# 一行ずつセットに追加
for line1 in lines1:
line1 = line1.rstrip(os.linesep)
set1.add(line1)
for line2 in lines2:
line2 = line2.rstrip(os.linesep)
set2.add(line2)
# サンプル2を正として、サンプル1の要素を削除します
for element2 in set2:
element2 = element2.rstrip(os.linesep)
set1.discard(element2)
print("Info | Difference below...")
# 一要素ずつ表示します
for element1 in set1:
element1 = element1.rstrip(os.linesep)
print("Info | {}".format(element1))
print("Info | Difference above...")
# ファイルをクローズします
file1.close()
file2.close()
Output:
Info | Current directory: C:\Users\むずでょ\OneDrive\ドキュメント\practice-py
Info | Current directory: c:\Users\むずでょ\OneDrive\ドキュメント\practice-py\diff1-100\ex4
Info | Difference below...
Info | Difference above...
「 ↑ ファイル2を正にすれば、ファイル1は全消しになるのは合ってるな☆」
Info | Current directory: C:\Users\むずでょ\OneDrive\ドキュメント\practice-py
Info | Current directory: c:\Users\むずでょ\OneDrive\ドキュメント\practice-py\diff1-100\ex4
Info | Difference below...
Info | Cat
Info | Difference above...
# 前後の空白を削除
line1 = line1.strip()
「 ↑しかしインデントの違いで 異なる情報と判定するのは 気が利かないな……☆ トリムするかだぜ☆?」
「 しかしお父ん、他にも CSV形式と RTF形式で データを持っているぜ☆?」
Is there a Python module for converting RTF to plain text? [closed]
「 RTF形式ファイルは駆逐しようぜ☆? パーサーはあるようだが……☆」
「 RTF形式ファイルは テキストファイルなんで、他人の作った ↑スクリプト で
スタイルを全部除去して プレーンテキストに変換しようぜ☆?」
'str' object has no attribute 'decode'
「 Python2 で書かれていて、 Python3 で動かね……☆ 修正するか……☆」
Before:
for match in pattern.finditer(text.decode()):
After
for match in pattern.finditer(text.replace(r'\_', '\u2010')):
print("Info | Current directory: {}".format(os.getcwd()))
os.chdir(os.path.dirname(os.path.abspath(__file__)))
print("Info | Current directory: {}".format(os.getcwd()))
args = sys.argv
# ファイル読取
file1 = open(args[1], "r")
file2 = open(args[2], "w")
# すべての内容を読み込みます
rtfText1 = file1.read()
# 処理を行います
plainText1 = striprtf(rtfText1)
# 表示します
print(
"""
Before----------------------------------------------------------------------------------------------------
{}
""".format(rtfText1))
print(
"""
After----------------------------------------------------------------------------------------------------
{}
""".format(plainText1))
# ファイル書き出し
file2.write(plainText1)
# ファイルをクローズします
file1.close()
file2.close()
Output:
Info | Current directory: C:\Users\むずでょ\product\repos\product-py
Info | Current directory: c:\Users\むずでょ\product\repos\product-py\extract-rtf1-100\ex1
Before----------------------------------------------------------------------------------------------------
{\rtf1}
After----------------------------------------------------------------------------------------------------
「 ↑ こんな風に プレーンテキストに変換していってくれるので、目視確認で1ファイルずつ処分していくかだぜ☆
After----------------------------------------------------------------------------------------------------
@@©¶@Ìc[@Ì ðµÄA
@@ðJ¢ÄÝܵå¤B
<書きかけ>
Crieitは個人で開発中です。
興味がある方は是非記事の投稿をお願いします! どんな軽い内容でも嬉しいです。
なぜCrieitを作ろうと思ったか
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください!