Perhaps the ones here who think I was trying to make you do my homework can
actually help me for real. Since I run my own company (not working for any
of the big ones) I can't afford official training in anything. So I teach
myself, help is always welcome and sought for. If that feels like doing
homework for me, so be it.
The fact is that I do try to learn Python. It can do things I thought
required much more coding. Look at the attached. It builds a concordance
table first. That was an excercise from a book on Pascal programming. In
Pascal the solution is 2 pages of code. In Python it is 8 lines. Beautiful!
Anybody catches any other ways to improve my program (attached), you are
most welcome. Help me learn, that is one of the objectives of this
newsgroup, right? Or is it all about exchanging the next to impossible
solution to the never to happen unreal world problems?
Regards,
Alex van der Spek
"D'Arcy J.M. Cain" <da...@druid.net> wrote in message
news:mailman.2159.1281917130.1673.python-l...@python.org...
On 15 Aug 2010 23:33:10 GMT
Steven D'Aprano <st...@remove-this-cybersource.com.au> wrote:
Under what possible circumstances would you prefer this code to the
built-
in str.join method?
I assumed that it was a trap for someone asking for us to do his
homework. I also thought that it was a waste of time because I knew
that twenty people would jump in with the correct answer because of
"finally, one that I can answer" syndrome.
--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
#! usr/bin/env python
# Merge log files to autolog file
import os
import fileinput
#top='C:\\Documents and Settings\\avanderspek\\My
Documents\\CiDRAdata\\Syncrude\\CSL\\August2010'
top='C:\\Users\\ZDoor\\Documents\\CiDRA\\Syncrude\CSL\\August2010'
i,j,k=0,0,0
date={}
fps=0.3048
tab='\t'
bt='-999.25'+'\t''-999.25'+'\t''-999.25'+'\t''-999.25'+'\t'+'-999.25'
al='Status'+'\t'+'State'+'\t'+'-999.25'
for root,dirs,files in os.walk(top):
#Build a concordance table of days on which data was collected
for name in files:
ext=name.split('.',1)[1]
if ext=='txt':
dat=name.split('_')[1].split('y')[1]
if dat in date.keys():
date[dat]+=1
else:
date[dat]=1
print 'Concordance table of days:'
print date
print 'List of files processed:'
#Build a list of same day filenames, 5 max for a profile meter,skip first
and last days
for f in sorted(date.keys())[2:-1]:
logs=[]
for name in files:
ext=name.split('.')[1]
if ext=='txt':
dat=name.split('_')[1].split('y')[1]
if dat==f:
logs.append(os.path.join(root,name))
#Open the files and read line by line
datsec=False
lines=[[] for i in range(5)]
fn=0
for line in fileinput.input(logs):
if line.split()[0]=='DataID':
datsec=True
ln=0
if datsec:
lines[fn].append(line.split())
ln+=1
if ln==10255:
datsec=False
fileinput.nextfile()
fn+=1
print fileinput.filename().rsplit('\\',1)[1]
fileinput.close()
aut='000_AutoLog'+f+'.log'
out=os.path.join(root,aut)
alf=open(out,'w')
alf.write('Timestamp (mm/dd/yyyy hh:mm:ss) VF 1 VF 2 VF 3
VF 4 VF 5 Q 1 Q 2 Q 3 Q 4 Q 5 Status State Metric
Band Temperature 1 Band Temperature 2 Band Temperature 3 Band
Temperature 4 Band Temperature 5 SPL 1 SPL 2 SPL 3 SPL 4 SPL
5'+'\n')
for wn in range(1,10255,1):
for i in range(5):
lines[i][wn][2]=str(float(lines[i][wn][2])/fps)
tp=lines[0][wn][0]+' '+lines[0][wn][1]
vf=tab.join([lines[i][wn][2] for i in range(5)])
vq=tab.join([lines[i][wn][3] for i in range(5)])
vs=tab.join([lines[i][wn][4] for i in range(5)])
#sf=tab.join([lines[i][wn][5] for i in range(5)])
#sq=tab.join([lines[i][wn][6] for i in range(5)])
#ss=tab.join([lines[i][wn][7] for i in range(5)])
alf.write(tp+'\t'+vf+'\t'+vq+'\t'+al+'\t'+bt+'\t'+vs+'\n')
alf.close()
print "Done"
--
http://mail.python.org/mailman/listinfo/python-list