Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write to the file with array values in python?

Tags:

python

string

>>> w
['Parts-of-speech', 'disambiguation', 'techniques', '(', 'taggers', ')',
 'are', 'often', 'used', 'to', 'eliminate', '(', 'or', 'substantially',
 'reduce', ')', 'the', 'parts-of-speech', 'ambiguitiy', 'prior', 'to',
 'parsing.', 'The', 'taggers', 'are', 'all', 'local', 'in', 'the', 'sense',
 'that', 'they', 'use', 'information', 'from', 'a', 'limited', 'context',
 'in', 'deciding', 'which', 'tag', '(', 's', ')', 'to', 'choose', 'for',
 'each', 'word.', 'As', 'is', 'well', 'known', ',', 'these', 'taggers',
 'are', 'quite', 'successful', '.']
>>> q=open("D:\unieng.txt","w")
>>> q.write(w)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument 1 must be string or read-only character buffer, not list
like image 247
chandrakanth duraisamy Avatar asked Dec 28 '22 02:12

chandrakanth duraisamy


1 Answers

Use writelines() method to write your content.

>>> f   = open("test.txt",'w')
>>> mylist = ['a','b','c']
>>> f.writelines(mylist)

file.writelines(sequence)

Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value

Note: writelines() does not add line separators.

like image 168
RanRag Avatar answered Dec 30 '22 09:12

RanRag