2017-09-03 5 views
0

txt 파일에 여러 가지를 쓰려고하는데, 어떤 이유로 새로운 줄에 각 항목을 가져올 수 없습니다. 나는 '\ n'을 다른 지점에 배치하는 것을 망쳤지만 그 결과는 여전히 동일합니다. 아래의 코드를 사용하고 있습니다 :Python 3.X txt 파일에 쓰기가 새 줄을 만들지 않습니다.

from collections import Counter 

File_1 = open('path1', 'r') 
wordCounter = Counter(File_1.read().lower().replace('<p>','').replace('<p><b>','').replace('</p>','').replace('</b>','').replace('.','').replace("'",'').replace('"','').replace('<i>','').replace('</i>','').replace(',','').replace('(','').replace('-','').replace(')','').replace('<b>','').replace(';','').split()) 
with open('path2','w') as File_2: 
    File_2.write('{:3} ==> {:15}'.format('Word','Count')) 
    File_2.write('-' * 18) 
    for (word,occurrence) in wordCounter.most_common(): 
     File_2.write('{:3} ==> {:15}'.format(word,occurrence)) 
File_1.close() 
File_2.close() 

시도는 내가 그것을 정렬 할 수 있습니다 전에 청소를해야하는 불안정한 텍스트 파일로 작업하고, 많은 전화를 대체 무시. 나는 각 항목과 같이 표시 할 : 나는 아마 여기에 신인 실수를하고 기분이

Word ==> Count ------------------Entry 1 ==> # of entriesEntry 2 ==> # of entries, etc. 

하지만, 얻을 수있는 쉬운 방법이있다 :

Word ==> Count 
Eighteen dashes 
Entry 1 ==> # of entries 
Entry 2 ==> # of entries 
etc. 

를 내가지고 결국 무엇입니까 새 항목에 각 항목을 쓰는 파일? 도움을 주셔서 미리 감사드립니다.

+4

잘못된 것입니다. 그것은'/ n'이 아니라'\ n'입니다. –

답변

1

앞에서 설명한 바와 같이 슬래시 (/)가 아닌 백 슬러 (\)를 사용합니다.

이것은 당신의 고정 코드 :

from collections import Counter 
File_1 = open('path1', 'r') 
wordCounter = Counter(File_1.read().lower().replace('<p>','').replace('<p><b>','').replace('</p>','').replace('</b>','').replace('.','').replace("'",'').replace('"','').replace('<i>','').replace('</i>','').replace(',','').replace('(','').replace('-','').replace(')','').replace('<b>','').replace(';','').split()) 
with open('path2','w') as File_2: 
    File_2.write('{:3} ==> {:15}\n'.format('Word','Count')) 
    File_2.write(str('-' * 18)+'\n') 
    for (word,occurrence) in wordCounter.most_common(): 
     File_2.write('{:3} ==> {:15}\n'.format(word,occurrence)) 

File_1.close() 

또한으로부터 않는,있는 File2에 가까운을 추가 할 필요가 없습니다 당신은

+0

이것은 해결책이었습니다. 잘못된 장소에 \ n 넣었습니다. 정말 고맙습니다. –

2

당신은에 재로 print() 기능을 사용할 수 있습니다에 대한 파일. close()에 호출에 대해 걱정할 필요가 없습니다 :

from collections import Counter 


with open('path1', 'r') as file_1: 
    wordCounter = Counter(file_1.read().lower() 
          .replace('<p>','').replace('<p><b>','') 
          .replace('</p>','').replace('</b>','') 
          .replace('.','').replace("'",'') 
          .replace('"','').replace('<i>','') 
          .replace('</i>','').replace(',','') 
          .replace('(','').replace('-','') 
          .replace(')','').replace('<b>','') 
          .replace(';','').split()) 

with open('path2','w') as file_2: 
    print('{:3} ==> {:15}'.format('Word','Count'), file=file_2) 
    print('-' * 18, file=file_2) 
    for word, occurrence in wordCounter.most_common(): 
     print('{:3} ==> {:15}'.format(word,occurrence), file=file_2) 

나는 또한 당신이 PEP 8 — the Style Guide for Python Code 따라하는 것이 좋습니다, 당신이 명명 규칙을

또한, 파일을 열 수있는 with statement를 사용하는 것이 좋습니다입니다 설명했다.

참고 : 당신이 당신의 스크립트의 상단에있는 __future__ 지시어를 사용할 수 있습니다, 파이썬 2와 print() 기능을 사용합니다.

from __future__ import print_function 
+0

그 질문에 대답하지 않습니다. 그는 개보수를 추가하려고합니다. –

+0

정보를 제공해 주셔서 감사 드리며, 스타일 가이드를보고 개선 할 수있는 부분을 살펴 보겠습니다. –

+0

@PokestarFan :'print()'함수는'end = ""'를 지정하지 않는 한 개행을 추가합니다. –