-1
我處理文本文件與Python windows下的結束,一些源文件的內容是這樣的:蟒蛇寫入線到文件避免換行符在文件
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
我將刪除所有空行與蟒蛇代碼:
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
非常感謝。