tags:

views:

108

answers:

3

I have extracted some data from the file and want to write it in the file 2 but the program says 'sequence item 1: expected string, list found', I want to know how I can convert buffer[] i.e. string into sequence, so that it could be saved in file 2.

file = open('/ddfs/user/data/k/ktrip_01/hmm.txt','r')
file2 = open('/ddfs/user/data/k/ktrip_01/hmm_write.txt','w')
buffer = []
rec = file.readlines()
for line in rec :
        field = line.split()
        print '>',field[0]
        term = field[0]
        buffer.append(term)
        print   field[1], field[2],   field[6], field[12]
        term1 = field [1]
        buffer.append(term1)
        term2 = field[2]
        buffer.append[term2]
        term3 = field[6]
        buffer.append[term3]
        term4 = field[12]
        buffer.append[term4]
        file2.write(buffer)
file.close()
file2.close()
+2  A: 
''.join(buffer)
blokeley
yes, though the OP probably wants a space or comma as a separator I'm guessing.
Justin Peel
Thanks for comment. In which case use `' '.join(buffer)` or `','.join(buffer)`
blokeley
sorry it still says that buffer is list , and the ' '.join(buffer) expect string..
PARIJAT
`str.join()` does not expect a string per se, it expects a _list_ of strings. The code you provided us, sans syntax errors, does exactly that. Maybe you should give us the actual code?
badp
@PARJAT, @bp, are you addressing the original poster or me? `buffer` is a list of strings and therefore `''.join(buffer)` will join them into one string.
blokeley
+6  A: 

Try str.join:

file2.write(' '.join(buffer))

Documentation says:

Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.

The MYYN
A: 
file2.write(','.join(buffer))
Tendayi Mawushe