안녕하세요,이 모든 코드는 실행하려고합니다. 나는 컴퓨터 과학자가 아니며 이것이 쉬운 대답이라는 것을 알고 있습니다. 나는 대답 할 수있는 도구가 없습니다. 이 목록을 텍스트 파일로 인쇄하려고합니다. 화면에 인쇄하면 작동합니다. 내가 오류는 이것이다 : "형식 오류 : 예상 문자 버퍼 오브젝트"여기python 출력 데이터 유형 혼동
코드
input = open('Tyger.txt', 'r')
text = input.read()
wordlist = text.split()
output_file = open ('FrequencyList.txt','w')
wordfreq = [wordlist.count(p) for p in wordlist]
#Pair words with corresponding frequency
dictionary = dict(zip(wordlist,wordfreq))
#Sort by inverse Frequency and print
aux = [(dictionary[key], key) for key in dictionary]
aux.sort()
aux.reverse()
for a in aux: output_file.write(a)
감사합니다!
이 방법으로 문제가 해결되지는 않지만 파일을 여는 데 [with with'statement] (http://docs.python.org/reference/compound_stmts.html#the-with-statement)를 사용해야합니다. 또한 항목을 계산하는 곳에서 ['collections.Counter'] (http://docs.python.org/library/collections.html#collections.Counter)를 살펴보고 사전 항목의 목록을 이해할 때 [ (값, 키) 키, 값 dictionary.items()]'- ['dict.items()'] 사용 (http://docs.python.org/library/stdtypes.html#dict.items) 의미 당신은 읽기 좋은 사전 검색을 계속 호출 할 필요가 없습니다. –
나는'output_file.write (a)'를'output_file.write (str (a))'로 바꿈으로써 시작할 것입니다. 화면에 인쇄 할 때 인쇄하기 전에 암시적인 문자열 변환을 수행합니다. 파일에 쓰는 것은 그런 일을하지 않습니다. –
무엇이'type (a)'인지 그것은 문자열이어야합니다. 'str (a)'를 사용하여 문자열에 캐스트 할 수 있습니다. – dm03514