<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
I also am having issues with this.<br><br>> Date: Sun, 31 Oct 2010 14:48:09 -0500<br>> From: python.list@tim.thechases.com<br>> To: iwawi123@gmail.com<br>> Subject: Re: text file reformatting<br>> CC: python-list@python.org<br>> <br>> > PRJ01001 4 00100END<br>> > PRJ01002 3 00110END<br>> ><br>> > I would like to pick only some columns to a new file and put them to a<br>> > certain places (to match previous data) - definition file (def.csv)<br>> > could be something like this:<br>> ><br>> > VARIABLE     FIELDSTARTS     FIELD SIZE      NEW PLACE IN NEW DATA FILE<br>> > ProjID    ;       1       ;       5       ;       1<br>> > CaseID     ;       6       ;       3       ;       10<br>> > UselessV  ;       10      ;       1       ;<br>> > Zipcode    ;       12      ;       5       ;       15<br>> ><br>> > So the new datafile should look like this:<br>> ><br>> > PRJ01    001       00100END<br>> > PRJ01    002       00110END<br>> <br>> <br>> How flexible is the def.csv format?  The difficulty I see with <br>> your def.csv format is that it leaves undefined gaps (presumably <br>> to be filled in with spaces) and that you also have a blank "new <br>> place in new file" value.  If instead, you could specify the <br>> width to which you want to pad it and omit variables you don't <br>> want in the output, ordering the variables in the same order you <br>> want them in the output:<br>> <br>>   Variable; Start; Size; Width<br>>   ProjID; 1; 5; 10<br>>   CaseID; 6; 3; 10<br>>   Zipcode; 12; 5; 5<br>>   End; 16; 3; 3<br>> <br>> (note that I lazily use the same method to copy the END from the <br>> source to the destination, rather than coding specially for it) <br>> you could do something like this (untested)<br>> <br>>    import csv<br>>    f = file('def.csv', 'rb')<br>>    f.next() # discard the header row<br>>    r = csv.reader(f, delimiter=';')<br>>    fields = [<br>>      (varname, slice(int(start), int(start)+int(size)), width)<br>>      for varname, start, size, width<br>>      in r<br>>      ]<br>>    f.close()<br>>    out = file('out.txt', 'w')<br>>    try:<br>>      for row in file('data.txt'):<br>>        for varname, slc, width in fields:<br>>          out.write(row[slc].ljust(width))<br>>        out.write('\n')<br>>    finally:<br>>      out.close()<br>> <br>> Hope that's fairly easy to follow and makes sense.  There might <br>> be some fence-posting errors (particularly your use of "1" as the <br>> initial offset, while python uses "0" as the initial offset for <br>> strings)<br>> <br>> If you can't modify the def.csv format, then things are a bit <br>> more complex and I'd almost be tempted to write a script to try <br>> and convert your existing def.csv format into something simpler <br>> to process like what I describe.<br>> <br>> -tkc<br>> <br>> <br>> -- <br>> http://mail.python.org/mailman/listinfo/python-list<br>                                          </body>
</html>