저는 파이썬에서 절대적인 초보자입니다. 나는 그리스어 놀이의 텍스트 분석을하고 각 단어의 단어 빈도를 세고있다. 연극은 매우 길기 때문에 전체 데이터 세트를 볼 수 없으며 파이썬 창에 공간이 충분하지 않기 때문에 빈도가 가장 낮은 단어 만 보여줍니다. 나는 그것을 .csv 파일로 변환하려고 생각하고있다. 내 전체 코드는 다음과 같습니다.사전 값을 CSV 파일로 변환하는 방법은 무엇입니까?
#read the file as one string and spit the string into a list of separate words
input = open('Aeschylus.txt', 'r')
text = input.read()
wordlist = text.split()
#read file containing stopwords and split the string into a list of separate words
stopwords = open("stopwords .txt", 'r').read().split()
#remove stopwords
wordsFiltered = []
for w in wordlist:
if w not in stopwords:
wordsFiltered.append(w)
#create dictionary by counting no of occurences of each word in list
wordfreq = [wordsFiltered.count(x) for x in wordsFiltered]
#create word-frequency pairs and create a dictionary
dictionary = dict(zip(wordsFiltered,wordfreq))
#sort by decreasing frequency and print
aux = [(dictionary[word], word) for word in dictionary]
aux.sort()
aux.reverse()
for y in aux: print y
import csv
with open('Aeschylus.csv', 'w') as csvfile:
fieldnames = ['dictionary[word]', 'word']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'dictionary[word]': '1', 'word': 'inherited'})
writer.writerow({'dictionary[word]': '1', 'word': 'inheritance'})
writer.writerow({'dictionary[word]': '1', 'word': 'inherit'})
인터넷에서 csv 코드를 발견했습니다. 내가 얻고 자하는 것은 가장 높은 주파수에서 가장 낮은 주파수까지의 전체 데이터 목록입니다. 지금 당장이 코드를 사용하면 파이썬은 csv 부분을 완전히 무시하고 CSV 코드를 작성하지 않은 것처럼 데이터를 인쇄하는 것처럼 보입니다.
내가 의도 한 결과를보기 위해 무엇을 코딩해야하는지에 대한 아이디어가 있습니까?
감사합니다.
파일을 닫아야합니다. – DrBwts