1

i have the following function

outFile = open("svm_light/{0}/predictions-average.txt".format(hashtag), "a")
with open('svm_light/{0}/predictions-{1}'.format(hashtag,segMent)) as f:
    tot_sum = 0
    for i,x in enumerate(f, 1):
        val = float(x)
        tot_sum += val            
        average = tot_sum/i
        outFile.write(average)  

I'm simply trying to print the output for each average to 1 average per line. however im getting the following error...

  outFile.write(average)            
TypeError: expected a character buffer object

if I simply change my program to this:

with open('svm_light/{0}/predictions-{1}'.format(hashtag,segMent)) as f:
    tot_sum = 0
    for i,x in enumerate(f, 1):
         val = float(x)
         tot_sum += val            
         average = tot_sum/i
         print average

prints the following:

  @ubuntu:~/Documents/tweets/svm_light$ python2.7 tweetAverage2.py

  0.428908289104
  0.326446277105
  0.63672940322
  0.600035561829
  0.666699795857

it prints the output neatly to the screen, but i would like to save it 1 average per line, much like is showing in the actual output.
Im new to python, and am using 2.7 under ubuntu.

UPDATE

thanx to a quick response, introduced the str function. However, it prints an empty file, i can see the file has contents for a bit, and then its gone. most likely its being overwritten the whole time. So im placing this print function somehwere it shouldnt be, but where?

1
  • 1
    outFile.write(str(average) + "\n") Commented Jun 7, 2013 at 17:56

1 Answer 1

3

You should convert average to a string before writing it to a file, you can use str() or string formatting for that.

outFile.write(str(average)) 

Help on file.write:

>>> print file.write.__doc__
write(str) -> None.  Write string str to file.  #expects a string

Note that due to buffering, flush() or close() may be needed before
the file on disk reflects the data written.

Update:

outFile_name = "svm_light/{0}/predictions-average.txt".format(hashtag)
in_file_name = 'svm_light/{0}/predictions-{1}'.format(hashtag,segMent)
with open(in_file_name) as f, open(outFile_name, 'w') as outFile:
    tot_sum = 0
    for i,x in enumerate(f, 1):
        val = float(x)
        tot_sum += val            
        average = tot_sum/i
        outFile.write(average + '\n') # '\n' adds a new-line  
Sign up to request clarification or add additional context in comments.

10 Comments

@RHK-S8 open the outFile in append mode('a'), if you're writing to the same file in a loop.
@RHK-S8 open the outFile using with statement, your code didn't work because you didn't close or flushed the outFile. Try my updated code.
i copied your code, it produces 1 line with 1 average. instead ofmultiple averages, per line
@RHK-S8 I guess I didn't indent the write part properly. But there are no multiple averages here, you just calculated one average at the end of the loop.
no probs :) see the update, it prints 5 averages (input was 5 files) to the screen, its that output i want in a file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.