reading a specific column from file
Chris
cwitts at gmail.com
Fri Jan 11 07:28:26 EST 2008
On Jan 11, 2:15 pm, cesco <fd.calabr... at gmail.com> wrote:
> Hi,
>
> I have a file containing four columns of data separated by tabs (\t)
> and I'd like to read a specific column from it (say the third). Is
> there any simple way to do this in Python?
>
> I've found quite interesting the linecache module but unfortunately
> that is (to my knowledge) only working on lines, not columns.
>
> Any suggestion?
>
> Thanks and regards
> Francesco
for (i, each_line) in enumerate(open('input_file.txt','rb')):
try:
column_3 = each_line.split('\t')[2].strip()
except IndexError:
print 'Not enough columns on line %i of file.' % (i+1)
continue
do_something_with_column_3()
More information about the Python-list
mailing list