[파이썬 Example #006] Text 파일 바꾸기 (Replace)
              
          2019. 10. 17. 14:16ㆍ컴퓨터_Com/파이썬 지식_Things to remember
1. 한 줄 바꾸기
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
 | 
 def replace_line_bum(file_name, search_text, replace_text): 
    listOfFile = [] 
    search_str = search_text.lower() 
    with open(file_name, 'rt') as f: 
        for line in f: 
            if line.lower().find(search_str) != -1: 
                listOfFile.append(replace_text) 
            else: 
                listOfFile.append(line.rstrip('\n')) 
    with open(file_name, 'w') as f: 
        for line in listOfFile: 
            f.writelines("%s\n" % line) 
 | 
cs | 
먼저 'with open'으로 텍스트 파일을 읽어서 ('rt') 한 줄씩 line에 집어 넣는다. 한 줄씩 집어 넣는 도중에 search_str을 만나면 (같으면 1, 아니면 -1), replace_text를 listOfFile 리스트에 집어 넣고, 만나지 못하면 line을 listOfFile에 집어 넣는다.
모든 줄이 listOfFile 리스트에 저장되면 그 리스트를 file_name에 한 줄씩 집어 넣으면서 파일로 만든다. 이 때 '\n'은 삭제하면서 파일을 만든다.
2. 여러 줄 바꾸기
| 
 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 
 | 
 def write_file_bum(file_name, search_text, current_map): 
    listOfFile = [] 
    listOfNewMap = [] 
    search_str = search_text.lower() 
    i = 0 
    with open(file_name, 'rt') as f: 
        for line in f: 
            i += 1 
            listOfFile.append(line) 
            if line.lower().find(search_str) != -1: 
                lineNum_ini = i 
    new_arr = np.savetxt('newmap.txt', current_map, fmt='%2d', delimiter=' ') 
    with open('newmap.txt', 'rt') as f: 
        for line in f: 
            listOfNewMap.append(line) 
    with open(file_name, 'w') as f: 
        i = 0 
        for line in listOfNewMap: 
            listOfFile[lineNum_ini + i] = line 
            i += 1 
        for line in listOfFile: 
            f.writelines("%s" % line) 
 | 
cs하 | 
한 줄 바꾸기와 유사하지만 current_map이 아래와 같이 numpy로 이루어진 배열일 때 쓴다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
 | 
 array([[ 2,  2, 10,  9,  4,  2,  6,  2,  3,  1], 
       [ 2,  9, 10,  6,  2,  4,  2,  6,  7,  1], 
       [10, 10,  6,  2,  3,  2, 10,  7,  2,  1], 
       [ 9,  6,  2,  6,  2,  6,  5,  2,  4,  1], 
       [ 4,  2,  3,  2,  9,  2,  8,  7,  1,  1], 
       [ 2,  4,  2,  6,  2,  6,  4,  7,  1,  0], 
       [ 6,  2, 10,  5,  8,  4,  7,  1,  1,  0], 
       [ 2,  6,  7,  2,  7,  7,  1,  1,  0,  0], 
       [ 3,  7,  2,  4,  1,  1,  1,  0,  0,  0], 
       [ 1,  1,  1,  1,  1,  0,  0,  0,  0,  0]]) 
 | 
cs | 
첫 번째 with open으로 file_name을 listOfFile로 만든다. 이 때, search_text를 search_str로 받아서 찾으면 줄 번호를 lineNum_ini로 기억한다.
new_arr는 배열로 된 current_map을 text파일로 만든다.
두 번째 with open은 with open으로 map text 파일을 listOfNewMap에 집어 넣는다.
세 번째 with open으로는 listOfNewMap list를 listOfFile에 집어 넣고 파일로 만든다.
'컴퓨터_Com > 파이썬 지식_Things to remember' 카테고리의 다른 글
| [파이썬 Example #008] Multi-run and editing output (0) | 2019.11.08 | 
|---|---|
| [파이썬 Example #007] numpy 다루기 (0) | 2019.10.17 | 
| [파이썬 Example #005] 디렉토리 다루기 (1) | 2019.10.16 | 
| [PyCharm #002] SciView 열기 (0) | 2019.10.16 | 
| [파이썬 Example #004] text파일의 특정 단어를 찾아서 새로운 파일에 쓰기 (0) | 2019.09.19 |