newcane
newcane

Reputation: 285

Outputting to a file

code snippet:

def after_equals(s):
    return s.partition(' = ')[-1]
for k,s in zip(keyword, score):
  print after_equals(k) + ',' + after_equals(s)

The output of the code prints:

NORTH,88466
GUESS,83965
DRESSES,79379
RALPH,74897
MATERIAL,68168

I need to output the into file. Please advice how to write to a file.

Upvotes: 0

Views: 125

Answers (2)

Fred Foo
Fred Foo

Reputation: 363597

with open(filename, 'w') as out:
    for k,s in zip(keyword, score):
        print >> out, after_equals(k) + ',' + after_equals(s)
        # note:  ^^^

Upvotes: 2

idanzalz
idanzalz

Reputation: 1760

Open a file in write mode and write a string to the file, then close it:

f = file('myfile.txt','w')
f.write(s)
f.close()

For your case:

def after_equals(s):
    return s.partition(' = ')[-1]
f = file('myfile.txt','w')
for k,s in zip(keyword, score):
  f.write('%s,%s\n' % (after_equals(k), after_equals(s)))
f.close()

Upvotes: 1

Related Questions