-1

I have done some basic Perl coding but never something in python. I would like to do the equivalent of sending the file to be read from in the command line option. This file is tab delimited, so split each column and then be able to perform some operation in those columns.

The perl code for doing this is

#!/usr/bin/perl

use warnings;
use strict;

while(<>) {
    chomp;
    my @H = split /\t/;

    my $col = $H[22];

    if($H[30] eq "Good") {
       some operation in col...
    }

    else {
        do something else
    }
}

What would be the python equivalent of this task?

Edit: I need the H[22] column to be a unicode character. How do I make col variable to be so?

sfactor
  • 12,592
  • 32
  • 102
  • 152

2 Answers2

3
#file: process_columns.py
#!/usr/bin/python

import fileinput

for line in fileinput.input():
    cols = l.split('\t')
    # do something with the columns

The snippet above can be used this way

./process_columns.py < data

or just

./process_columns.py data
satoru
  • 31,822
  • 31
  • 91
  • 141
2

Related to: Python equivalent of Perl's while (<>) {...}?

#!/usr/bin/env python
import fileinput

for line in fileinput.input():
    line = line.rstrip("\r\n")    # equiv of chomp
    H = line.split('\t')

    if H[30]=='Good':
        # some operation in col

        # first - what do you get from this?
        print repr(H[22])

        # second - what do you get from this?
        print unicode(H[22], "Latin-1")
    else:
        # do something else
        pass  # only necessary if no code in this section

Edit: at a guess, you are reading a byte-string and must properly encode it to a unicode string; the proper way to do this depends on what format the file is saved in and what your localization settings are. Also see Character reading from file in Python

Community
  • 1
  • 1
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • one more thing i need the H[22] column to be a unicode character. How do I make col variable to be so? – sfactor Jan 05 '11 at 02:08
  • It's not clear exactly what you mean by that, but if you're talking about unicode literals: http://docs.python.org/howto/unicode.html#unicode-literals-in-python-source-code – Paul Bissex Jan 05 '11 at 02:42
  • @pbx I need the H[22] column string to be unicode. I tried doing col = u"H[22]" but that doesn't work. I need the value stored in H[22] as an unicode character. – sfactor Jan 05 '11 at 02:50
  • @sfactor - what is the encoding of the character in the file? eg. if it's 'latin1', you use H[22].decode('latin1') to get a unicode object – detly Jan 05 '11 at 03:15
  • @Hugh in both the cases i get the column string, in the first case enclosed in single quotes. the file is a unix ASCII format file. – sfactor Jan 05 '11 at 03:58
  • @sfactor - if it's in ASCII, why do you need a unicode object? At any rate, H[22].decode('ascii') should work fine. – detly Jan 05 '11 at 04:09
  • @sfactor: if the file is an ASCII file, how did you save a 'unicode character' in it? – Hugh Bothwell Jan 05 '11 at 05:14
  • 3
    @sfactor I really think you don't understand what Unicode is or how it works. Please read http://www.joelonsoftware.com/articles/Unicode.html , and then ask a new question. – Karl Knechtel Jan 05 '11 at 06:57
  • @Karl and @Hugh I will have a look at those resources, the thing is I am using an external python package and it insists that the col string that I send is a unicode character otherwise it gives an error complaining it is not a unicode character. But I have a working code now thanks. – sfactor Jan 05 '11 at 12:30