Column data with first value from each row as key?
Irvine, Wayne D
Wayne.D.Irvine at BHPBilliton.com
Mon May 16 00:28:43 EDT 2005
Hi All,
I have a file of column data where the first row/line of the file
contains the column headings. These I need to use as keys. Furthermore I
need to be able to use the first value from each of the data rows/lines
as a secondary key. This first column is called LineNumber and the rest
of the columns of which there are 400+ I will call data columns. So what
I am wanting to do is read the file which is tab-delimited (I can do
this alright) and then be able to do things such as select all values
from the data columns where LineNumber = xxxxx. I assume I want to use
lists but I am unsure how to achieve the above. I have found the
following example which seems to partly do this but it only seems to
read the first line of my input file. And I can't see how to reference
my LineNumbers by their respective value.
### Start of code ###
import UserDict
class SeqDict(UserDict.DictMixin):
"""Dictionary lookalike implemented with lists."""
def __init__(self):
self.keylist = []
self.valuelist = []
def __getitem__(self, key):
try:
i = self.keylist.index(key)
except ValueError:
raise KeyError
return self.valuelist[i]
def __setitem__(self, key, value):
try:
i = self.keylist.index(key)
self.valuelist[i] = value
except ValueError:
self.keylist.append(key)
self.valuelist.append(value)
def __delitem__(self, key):
try:
i = self.keylist.index(key)
except ValueError:
raise KeyError
self.keylist.pop(i)
self.valuelist.pop(i)
def keys(self):
return list(self.keylist)
# Now, read a comma-delimited file into the dictionary
def readfile():
mydict = SeqDict()
# NOTE - enter the path and filename WITHOUT quotes around it.
filename = raw_input('Enter name of file to open: ')
file = open(filename, 'r')
linenum=0
for line in file.readlines():
linenum+=1
mywords = string.split(line, sep='\t')
for word in mywords:
if linenum == 1:
# First row of data: the dictionary keys
mydict.keylist.append(word)
# mydict.valuelist.append([])
else:
# The rest of the data
mydict.valuelist.append(word)
for key, value in mydict.iteritems():
print key, value
if __name__ == '__main__':
readfile()
### End of code ###
Cheers,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050515/42b6c1bf/attachment.html>
More information about the Python-list
mailing list