ubuntu_noob
ubuntu_noob

Reputation: 2365

write the elements of list to file

Bigram is a list which looks like-

[('a', 'b'), ('b', 'b'), ('b', 'b'), ('b', 'c'), ('c', 'c'), ('c', 'c'), ('c', 'd'), ('d', 'd'), ('d', 'e')]

Now I am trying to wrote each element if the list as a separate line in a file with this code-

 bigram = list(nltk.bigrams(s.split()))
 outfile1.write("%s" % ''.join(ele) for ele in bigram)

but I am getting this error :

TypeError: write() argument must be str, not generator

I want the result as in file-

('a', 'b') 
('b', 'b')
('b', 'b')
('b', 'c')
('c', 'c')
......

Upvotes: 3

Views: 146

Answers (3)

9000
9000

Reputation: 40894

This is the operator precedence problem.

You want an expression like this:

("%s" % ''.join(ele)) for ele in bigram

Instead, you get it interpreted like this, where the part in the parens is indeed a generator:

"%s" % (''.join(ele) for ele in bigram)

Use the explicit parentheses.

Please note that ("%s" % ''.join(ele)) for ele in bigram is itself a generator. You need to call write on each element from it.

If you want to write each pair in a separate line, you have to add line separators explicitly. The easiest, to my mind, is an explicit loop:

for pair in bigram:
  outfile.write("(%s, %s)\n" % pair)

Upvotes: 1

javidcf
javidcf

Reputation: 59731

Try this:

outfile1.writelines("{}\n".format(ele) for ele in bigram)

Upvotes: 1

Jean-François Fabre
Jean-François Fabre

Reputation: 140276

you're passing a generator comprehension to write, which needs strings.

If I understand correctly you want to write one representation of tuple per line.

You can achieve that with:

outfile1.write("".join('{}\n'.format(ele) for ele in bigram))

or

outfile1.writelines('{}\n'.format(ele) for ele in bigram)

the second version passes a generator comprehension to writelines, which avoids to create the big string in memory before writing to it (and looks more like your attempt)

it produces a file with this content:

('a', 'b')
('b', 'b')
('b', 'b')
('b', 'c')
('c', 'c')
('c', 'c')
('c', 'd')
('d', 'd')
('d', 'e')

Upvotes: 3

Related Questions