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?
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?
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
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!).
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)