views:

2059

answers:

3

I have a file that contains the symbol table details.Its in the form of rows and columns.

I need to extract first and last column.

How can I do that?

+2  A: 

What type of delimiter are you using? That is, what separates your columns?

I'll assume you're using comma delimiters, like so:

col1,  col2,  col3
col11, col12, col13
col21, col22, col23
col31, col32, col33

The following code will parse it and print the first and last columns of each row:

# open file to read
f = file('db.txt', 'r')

# iterate over the lines in the file
for line in f:
    # split the line into a list of column values
    columns = line.split(',')
    # clean any whitespace off the items
    columns = [col.strip() for col in columns]

    # ensure the column has at least one value before printing
    if columns:
     print "first", columns[0]  # print the first column
     print "last", columns[-1] # print the last column
Soviut
For csv files, you should be using the csv module.
Aaron Gallagher
@Aaron, the author didn't specify that he's using CSV, so Soviut presented a good solution because it shows what is going on and therefore can be modified more easily.
Evan Fosmark
@Soviut: Using "," is confusing, since CSV is well-defined and isn't this. Using " " is a better choice because it doesn't overlap CSV.
S.Lott
+2  A: 

The most convenient way of parsing tables written to text files is using the csv module. It supports any delimiter and is more convenient to use than manual line-by-line parsing. Example:

import csv

def get_first_and_last_column(filename, separator):
    with file(filename, 'rb') as file_obj:
        for line in csv.reader(file_obj, 
              delimiter=separator,    # Your custom delimiter.
              skipinitialspace=True): # Strips whitespace after delimiter.
            if line: # Make sure there's at least one entry.
                yield line[0], line[-1]

if __name__ == '__main__':
    for pair in get_first_and_last_column(r'c:\temp\file.txt', ';'):
        print pair

Now, if you give it a file like this:

Edgar; Alan; Poe
John; Smith

Lots;   of;   whitespace; here

It will produce the following output:

('Edgar', 'Poe')
('John', 'Smith')
('Lots', 'here')

EDIT: custom parameters to csv.reader can be passed as keyword arguments, too (thanks, nosklo!).

DzinX
It is needlessy complex - If you're doing something simple, you don't have to create a new dialect, just pass the params: csv.reader(file_obj, delimiter='|', skipinitialspace=True).
nosklo
Cool, thanks, I didn't know that :-)
DzinX
+3  A: 

csv module is the easier way. You can use any separator with this code:

import csv

def import_text(filename, separator):
    for line in csv.reader(open(filename), delimiter=separator, 
                           skipinitialspace=True):
        if line:
            yield line

for data in import_text('somefile.txt', '/'):
    print (data)
nosklo
+1 for using a standard module
davidavr
What does "if line" do? Skip empty lines?
pufferfish
pufferfish: yes.
nosklo