Printing to a file in python

932 views python
6

I have the below program wherein I am trying to convert text files to a character unigram(feature vector) and writing the output to a text file.

I am printing the output on the console and writing it to a text file at the same time, however, printing to the console will print all the records while printing to the file prints only the last iteration of the filename in articles.

Should I be using an array for rawcu, Please suggest?

Thanks in advance.

My Code:

for fileName in allarticles:
    rawcu = [0.0]*95
    out=open("CASIS-25fvs_rawcu.txt","w")
    fileOpen = open(fileName)
    charFrequency = {}
    for line in fileOpen:
        for letter in line:
            if((ord(letter) > 31) and ord(letter) < 127):
                rawcu[ord(letter)-32] += 1.0

    print rawcu
    print >> out, rawcu

answered question

1 Answer

8

You opened the file for over-writing, not for appending. Must be:

open("CASIS-25fvs_rawcu.txt", "a")

posted this

Have an answer?

JD

Please login first before posting an answer.