我在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)
我的问题是:
- 已处理文件末尾有一个换行符,我该如何避免它?
- 我还想删除这样的行:
- 第五列相等
- 和第八列的和等于0
多谢。