Windows で Python を使用してテキスト ファイルを処理しています。ソース ファイルの一部の内容は次のようになります。
FSZHB1 04 2012-11-24 1346 S000009106 BC14D01137 0 788 0 0 0 788
FSZHB1 04 2012-11-24 1425 S000009107 BC14D01587 0 1088 0 0 0 1088
FSZHB1 04 2012-11-24 1425 S000009107 BC14D99998 0 -8468 0 0 0 -8468
FSZHB1 04 2012-11-24 1425 S000009107 BC14D02045 0 3690 0 0 0 3690
FSZHB1 04 2012-11-24 1425 S000009107 BC14D02087 0 3690 0 0 0 3690
FSZHB1 04 2012-11-24 1702 S000009108 BC14D01900 0 1690 0 0 0 1690
FSZHB1 04 2012-11-24 1702 S000009108 BC14D02106 0 4690 0 0 0 4690
FSZHB1 04 2012-11-24 1702 S000009108 BC14D00653 0 1680 0 0 0 1680
FSZHB1 04 2012-11-24 1702 S000009108 BC14D99996 0 -10000 0 0 0 -10000
FSZHB1 04 2012-11-24 1702 S000009108 BC14D99996 0 10000 0 0 0 10000
FSZHB1 04 2012-11-24 1702 S000009108 BC14D01601 0 228 0 0 0 228
FSZHB1 04 2012-11-24 1702 S000009108 BC14D99998 0 -5968 0 0 0 -5968
FSZHB1 04 2012-11-24 1702 S000009108 BC14D02046 0 3990 0 0 0 3990
FSZHB1 04 2012-11-24 1702 S000009108 BC14D02045 0 3690 0 0 0 3690
FSZHB1 04 2012-11-24 2041 S000009109 BC14D01721 0 1183 0 0 0 1183
FSZHB1 04 2012-11-24 2041 S000009109 BC14D01892 0 903 0 0 0 903
FSZHB1 04 2012-11-24 2121 S000009110 BC14D02114 0 16900 0 0 0 16900
FSZHB1 04 2012-11-24 2121 S000009110 BC14D01898 0 256 0 0 0 256
FSZHB1 04 2012-11-24 2121 S000009110 BC14D99998 0 -7284 0 0 0 -7284
FSZHB1 04 2012-11-24 2121 S000009110 BC14D99997 0 5000 0 0 0 5000
FSZHB1 04 2012-11-24 2121 S000009110 BC14D99996 0 -10000 0 0 0 -10000
FSZHB1 04 2012-11-24 2121 S000009110 BC14D01652 0 128 0 0 0 128
Python コードですべての空白行を削除します。
def rem_blanklines(fings = None,c_path =None):
if os.path.isfile(fings):
tf = tempfile.NamedTemporaryFile(dir = c_path,delete = False)
blank_p = re.compile('\S')
with tf,open(fings) as f_obj:
for fl in f_obj:
if blank_p.match(fl):
fl = (fl.strip() + '\r\n').encode('UTF-8')
tf.write(fl)
bakfile = fings + '.bak'
os.rename(fings,bakfile)
os.rename(tf.name,fings)
os.remove(bakfile)
for x in os.listdir(wkd):
xx = os.path.join(wkd,x)
if os.path.isfile(xx):
rem_blanklines(fings = xx,c_path = wkd)
私の質問は次のとおりです。
- 処理されたファイルの最後に改行記号がありますが、どうすれば回避できますか?
- 次のような行も削除したい:
- 5列目は等しい
- 8 番目の列の合計は 0 です
どうもありがとう。