Flower in my dev/Python

<PYTHON>파싱결과 비교(split count) --> 파일 저장

꽃선생 2015. 5. 29. 09:59

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os, re
 
nPath = '경로/new_device'
oPath = '경로/old_device'
 
nList = os.listdir(nPath)
oList = os.listdir(oPath)
 
nList.sort()
oList.sort()
 
nFile = open('./flower_newcol.txt''w')
oFile = open('./flower_oldcol.txt''w')
 
nCheck = re.compile(r'flower_new\d*')
oCheck = re.compile(r'flower_old\d*')
 
for n in nList:
    if nCheck.search(n) != None:
        nFile.write(n + '\n')
        fPath = os.path.join(nPath, n)
        fRead = open(fPath, 'r')
        coList = fRead.readlines()
        for col in coList:
            s = col.split()
            cnt = len(s)
            nFile.write(str(cnt) + ' ')
        nFile.write('\n')
    else:
        pass
 
for o in oList:
    if oCheck.search(o) != None:
        oFile.write(o + '\n')
        fPath = os.path.join(oPath, o)
        fRead = open(fPath, 'r')
        coList = fRead.readlines()
        for col in coList:
            s = col.split()
            cnt = len(s)
            oFile.write(str(cnt) + ' ')
        oFile.write('\n')
    else:
        pass
cs

 

파싱된 결과의 전체 라인을 읽어서

 

라인별로 slpit 후 길이를 비교하여 결과를 파일로 저장하는 스크립트