<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Ok. I will try it and let you know. Thanks a lot!!<br><br>J<br><br>> Date: Tue, 30 Nov 2010 20:32:56 -0600<br>> From: python.list@tim.thechases.com<br>> To: javiervandam@gmail.com<br>> CC: python-list@python.org<br>> Subject: Re: Reading by positions plain text files<br>> <br>> On 11/30/2010 08:03 PM, javivd wrote:<br>> > On Nov 30, 11:43 pm, Tim Harig<user...@ilthio.net> wrote:<br>> >>> VARIABLE NAME POSITION (COLUMN) IN FILE<br>> >>> var_name_1 123-123<br>> >>> var_name_2 124-125<br>> >>> var_name_3 126-126<br>> >>> ..<br>> >>> ..<br>> >>> var_name_N 512-513 (last positions)<br>> >><br>> > and no, MRAB, it's not the similar problem (at least what i understood<br>> > of it). I have to associate the position this file give me with the<br>> > variable name this file give me for those positions.<br>> <br>> MRAB may be referring to my reply in that thread where you can do <br>> something like<br>> <br>> OFFSETS = 'offsets.txt'<br>> offsets = {}<br>> f = file(OFFSETS)<br>> f.next() # throw away the headers<br>> for row in f:<br>> varname, rest = row.split()[:2]<br>> # sanity check<br>> if varname in offsets:<br>> print "[%s] in %s twice?!" % (varname, OFFSETS)<br>> if '-' not in rest: continue<br>> start, stop = map(int, rest.split('-'))<br>> offsets[varname] = slice(start, stop+1) # 0-based offsets<br>> #offsets[varname] = slice(start+1, stop+2) # 1-based offsets<br>> f.close()<br>> <br>> def do_something_with(data):<br>> # your real code goes here<br>> print data['var_name_2']<br>> <br>> for row in file('data.txt'):<br>> data = dict((name, row[offsets[name]]) for name in offsets)<br>> do_something_with(data)<br>> <br>> There's additional robustness-checks I'd include if your <br>> offsets-file isn't controlled by you (people send me daft data).<br>> <br>> -tkc<br>> <br>> <br>> <br>> <br> </body>
</html>