Hey guys,

I wrote a file the other day, which reads in data, adds a column, and then reads the data out to a specific file. I thought everything was working great, with all of your help; however, I am now running into another problem.

The output data looks like this:

Which looks like a list; however, I think each character in the line is an element of the list. For example, if I do len(line) it tells me the line is 140 or so elements long. Further more, if I do print line[6], it print a quotation mark, '

What I need is data to be formatted like this:
585 266 322 147 0 469 chr1 ...

I have the options to change either the output file, or the program which generates it. Here is the program which generates it:

infile = open('rep_small.bed')

def get_names(line):
	'''Will scan the repElement name in a line, and if it is not already known, will save it.  Use to get all names in a file.'''	
	temp_name.append(line[11])
	return temp_name


def add_age(line):
	'''Calculates the age (milliDiv + milli... + milli)'''
	age =  int(line[2]) + int(line[3]) + int(line[4])
	return str(age)
for line in infile:
		line = line.strip()
		sline = line.split()
		'''call the age method'''
		calc_age = add_age(sline)		
		'''put the calculated age into the list at position 5'''		
		sline.insert(5, calc_age)
		print (sline)
		'''Call get_names method'''
		all_names=get_names(sline)
		for name in Name_List:
			if str(sline[11]) == str(name):
				outfile = '/home/hugadams/Desktop/SHANE_STUFF/Modules/Test1/' + name + '.txt'
		                o = open(outfile, 'a')
		                o.write(str(sline) + '\n')

Anybody see where I could fix this so that it outputs data better?

P.S. I know a solution exists in which I write the infile into a dictionary, with each line being a list of values, and keyed by chromosomes; however, if I can avoid this approach, I'd prefer to.

Well, I've defined a new method which parses the data a bit better. I think this may be the solution:

ef output(line):
''''''
for value in line:
outstring = str(line[0]) + " " + str(line[1]) + " " + str(line[2]) + " " + str(line[3])

Data seems formatted better

Well, I've defined a new method which parses the data a bit better. I think this may be the solution:

ef output(line):
''''''
for value in line:
outstring = str(line[0]) + " " + str(line[1]) + " " + str(line[2]) + " " + str(line[3])

Data seems formatted better

.join will work better for that.
outstring =" ".join(line)
You can also do something like

for element in len(line):
   fp.write("%s " % (str(line[element])))
fp.write( "\n")

Hey guys,

I wrote a file the other day, which reads in data, adds a column, and then reads the data out to a specific file. I thought everything was working great, with all of your help; however, I am now running into another problem.

The output data looks like this:

Which looks like a list; however, I think each character in the line is an element of the list. For example, if I do len(line) it tells me the line is 140 or so elements long. Further more, if I do print line[6], it print a quotation mark, '

What I need is data to be formatted like this:
585 266 322 147 0 469 chr1 ...

I have the options to change either the output file, or the program which generates it. Here is the program which generates it:

infile = open('rep_small.bed')

def get_names(line):
	'''Will scan the repElement name in a line, and if it is not already known, will save it.  Use to get all names in a file.'''	
	temp_name.append(line[11])
	return temp_name


def add_age(line):
	'''Calculates the age (milliDiv + milli... + milli)'''
	age =  int(line[2]) + int(line[3]) + int(line[4])
	return str(age)
for line in infile:
		line = line.strip()
		sline = line.split()
		'''call the age method'''
		calc_age = add_age(sline)		
		'''put the calculated age into the list at position 5'''		
		sline.insert(5, calc_age)
		print (sline)
		'''Call get_names method'''
		all_names=get_names(sline)
		for name in Name_List:
			if str(sline[11]) == str(name):
				outfile = '/home/hugadams/Desktop/SHANE_STUFF/Modules/Test1/' + name + '.txt'
		                o = open(outfile, 'a')
		                o.write(str(sline) + '\n')

Anybody see where I could fix this so that it outputs data better?

P.S. I know a solution exists in which I write the infile into a dictionary, with each line being a list of values, and keyed by chromosomes; however, if I can avoid this approach, I'd prefer to.

With your indentations being such a sloppy mess, I won't bother.

This article has been dead for over six months. Start a new discussion instead.