From: susana...@hotmail.com
To: tutor@python.org
Date: Thu, 12 Jul 2012 17:45:31 +0200
Subject: [Tutor] extracting a column from many files




Hi!
 
I want to extract from certain kind of files  column number 5 and 6. I want to 
extract acolumns number 5 to one file, and anumber6 to another file. 
I was following this example 
http://mail.python.org/pipermail/tutor/2009-February/067400.html , and under my 
understanding I created my own code wich gives me the following errors:
 


This code gives me 2 files:                ro with 2 lines
                bf empty
 
I attached my code, with two options (though I was trying wich much more)
Could be awesome If I extrace them in an excel file, Any suggestion????
 
Many thanks!!!
Susana

_______________________________________________ Tutor maillist - 
Tutor@python.org To unsubscribe or change subscription options: 
http://mail.python.org/mailman/listinfo/tutor                                   
 
#! /usr/bin/env python
#-*- coding: utf -8 -*-


import os
import fnmatch
import csv

path = '//../my_working_folder/'

files=os.listdir(path)
ro=[]; bf=[]
A=[]; B=[]

for infile in files:
        if fnmatch.fnmatch(infile, 'bb_*'):
                filename= os.path.join(path,infile)
                f=open(filename, 'r')

##########OPTION ONE            
                for line in f.readlines():
                        b=line.split('\t')
                        for b in line:
                                A.extend('\t'.join.b[5])
                                B.extend('\t'.join.b[6])
                ro.extend(A)
                bf.extend(B)
                f.close()

                lists=[(ro, 'ro'), (bf, 'bf')]
                for values, name in lists:
                        output=open( name + '.txt',  'w')
                        for value in values:
                                output.write(str(values))
                                output.write('\n')
                                output.close
                

##########OPTION TWO
                
for line in f.readlines():
                        
                        b=line.split('\t')
                        A.extend(b[5])
                        B.extend(b[6])
        ro.extend(A)
        bf.extend(B)
        
#the same: I get Syntaxerror:invalid syntax
        
        lists=[(ro, 'ro'), (bf, 'bf')]
        for values, name in lists:
                output=open( name + '.txt',  'w')
                for value in values:
                        output.write(str(values))
                        output.write('\n')

                output.close()
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to