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, 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) return temp_name def add_age(line): '''Calculates the age (milliDiv + milli... + milli)''' age = int(line) + int(line) + int(line) 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) == 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.