Hey guys,

Have been working on this tiny simple code for an hour and can't figure out how to fix it. The code does this:
Reads in 3-column, tab-delimited data file
Adds "1000" in the fourth column to every line
Writes out the 4-column file

def columns(infile, outfile):
	f = open(infile,'r')
	o = open(outfile,'w')

	temp = []   #Store an empty list
 
	for line in f:
		line=line.strip()
		line=line.split('\t')
		line.insert(3, "1000")
		temp.append(line)
#		print temp	 
 	
	o.write('\n'.join(temp)) 	
     	
	 

	print "See %s" % (outfile)    

	f.close()
	o.close()

And getting the error:
TypeError: sequence item 0: expected string, list found

I realize I'm trying to writeout a list and it is barking, but what can I do to fix it? I had an almost identical code run which only had one column of data to work with, but with multiple columns, it won't writeout.

TypeError: sequence item 0: expected string, list found

It is very difficult to tell without knowing which line is causing the error. A guess would be this line
o.write('\n'.join(temp))
as you are tyring to join a list of lists [from the split()] instead of one list. Try this instead (there is no reason to append to a list and then write the list)

def columns(infile, outfile):
    f = open(infile,'r')
    o = open(outfile,'w')

    temp = []   #Store an empty list
    for line in f:
        line=line.strip()
        line_list=line.split('\t')
        line_list.insert(3, "1000")
##		temp.append(line)
#		print temp	 
	
        o.write(' '.join(line_list))
        o.write("\n")

    f.close()
    o.close()

Obviously, I do not have the input file, so can not test, but this is the general idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.