[Tutor] csv files

Lloyd Kvam pythonTutor at venix.com
Sat Apr 17 15:06:02 EDT 2004


Here are some suggestions for rewriting the process function:

change inp1,inp2,... to a list.  I'll call it inplist from here on.

Keep the 76 line assumption for now.

curlines = [inp.readline() for inp in inplist] 
# read files in parallel into a list of current lines

outdata1 = [line[88:96] for line in curlines] 
# pull out data from each line

outlabel = '"' + curlines[0][:12] + '"' 
# pull out the lable value from first line and add quotes
# that line is not so readable, but I think it is OK

outstring = ','.join([outlabel] + [outdata])
# make label + data into a list.
# make the list into a string with comma separators

Now you can write the string to the output file.

use the same kind of logic for outp2.  Notice that the four lines above
would be exactly the same for outp2 except for the slice values 88:96
and 123:130.  So rather than cutting and pasting those lines and
changing the slice value, you could make them into a function that
returns the string to be written.

To call process with a list of input files:
process([inp1,inp2,inp3,inp4,inp5,inp6,inp7,inp8,inp9,inp10],outp1,outp2)
simply put [] square brackets around the input files.

I used the list comprehension syntax for processing the inplist.  Though
it may look a little strange as compared to C or Java code, it is a very
nice syntax for making new lists from existing lists.

Note that you never use the csv module.  It doesn't look like you need
it for this problem.


On Fri, 2004-04-16 at 20:39, ABDUL JABBAR SIDDIQUE wrote:
> As I mentioned in my last mail that I am new to programming and may
> ask stupid questions. :)Bare with me please.
> 
> I have coded as follows to convert some text files into csv format for
> onward reading into python programming. It seems that there must be
> some short way to do all this which I don't know. I will
> appreciate your advices. Following is the code:
> 
> from csv import *
> 
> def
> process(inp1,inp2,inp3,inp4,inp5,inp6,inp7,inp8,inp9,inp10,outp1,outp2):   
>     for i in range(76):    # it means i from 0 to 75
>         line1=inp1.readline()
>         line2=inp2.readline()
>         line3=inp3.readline()
>         line4=inp4.readline()
>         line5=inp5.readline()
>         line6=inp6.readline()
>         line7=inp7.readline()
>         line8=inp8.readline()
>         line9=inp9.readline()
>         line10=inp10.readline()
>             
>         if i >= 5:          ### Data values begin from line 5
>            
> outp1.write(('"'+line1[:12]+'"'+','+line1[88:96]+','+line2[88:96]+','+line3[88:96]+','+line4[88:96]+',' \
>                        
> +line5[88:96]+','+line6[88:96]+','+line7[88:96]+','+line8[88:96]+',' \
>                         +line9[88:96]+','+line10[88:96])+"\n")  ###
> Data values for Control Delays (MOE) is written to output file
> 
>            
> outp2.write(('"'+line1[:12]+'"'+','+line1[123:130]+','+line2[123:130]+','+line3[123:130]+','+line4[123:130]+',' \
>                        
> +line5[123:130]+','+line6[123:130]+','+line7[123:130]+','+line8[123:130]+',' \
>                         +line9[123:130]+','+line10[123:130])+"\n")
>               ### Data values for Speed (MOE) is written to output
> file
> 
> inp1 = open("S_1.OUT","r")      ### Results of Simulation output are
> named as S_1.OUT, S_2.OUT etc.
> inp2 = open("S_2.OUT","r")
> inp3 = open("S_3.OUT","r")
> inp4 = open("S_4.OUT","r")
> inp5 = open("S_5.OUT","r")
> inp6 = open("S_6.OUT","r")
> inp7 = open("S_7.OUT","r")
> inp8 = open("S_8.OUT","r")
> inp9 = open("S_9.OUT","r")
> inp10 = open("S_10.OUT","r")
> 
> outp1 = open ("CD.csv","w")
> outp2 = open ("Speed.csv","w")
> 
> 
> process(inp1,inp2,inp3,inp4,inp5,inp6,inp7,inp8,inp9,inp10,outp1,outp2)
> 
> 
> print "data copied to file..."        ### Mentioning that process of
> data extraction is completed.
> 
> # Now close the files
> inp1.close()
> inp2.close()
> inp3.close()
> inp4.close()
> inp5.close()
> inp6.close()
> inp7.close()
> inp8.close()
> inp9.close()
> inp10.close()
> ### Finally output file is closed.
> outp1.close()
> outp2.close()
> 
> Jabbar
> 
> 
> 
> ______________________________________________________________________
> Protect your PC - Click here for McAfee.com VirusScan Online  
> 
> ______________________________________________________________________
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list