[Tutor] extract information from txtfile

Alan Gauld alan.gauld at btinternet.com
Wed Oct 5 20:09:08 CEST 2011


On 04/10/11 21:00, Anna Olofsson wrote:

> The table looks something like this, but with many more columns and rows:
>
> # chr Pos ID REF ALT . . . . . . . . . . .
> 1 13645 - C T
> . . . . .
>
> I want to extract certain data from this table above and make it to a
> sentence looking something like this: chr1:13645 C/T
>
> import OS
> open vcf_file.vcf
> for each line
> words = split(line) # words[0] = 1 , words[1]=Pos , words[3]=REF ,
> words[4]=ALT
> if(words[0] not "#"): #Sometimes the word starts with a hash and I don't
> str="chr" + words[0] + ":" + words[1] + "\t" + words[3] + "/" + words[4]
> score = OS.system(str)

So basically you are trying to construct an OS command from the file 
data and then execute it and store the result?

You are pretty close. Some things to note when you try to convert it to 
real code:
1) the module is os not OS (but see below...).
2) you can say
	for line in open(filename):
3) Python uses the object.method style so split(line)
    become line.split()
4) use != rather than not in the if test
5) str is a builtin type so to avoid hiding it use another
    name (command maybe?)
6) os.system only returns the error code, to capture the output
    you should look at the subprocess module(see the documentation
    for examples)

Other than that the translation should be pretty close to what you have. 
Try it and come back if you get stuck.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list