Tutors,

Whilst having a play around with reading in textfiles and reformatting them I 
tried to write a python 3.2 script to read a CSV file, looking for any records 
which were short (indicating that the data may well contain an embedded CR/LF. 
I've attached a small sample file with a "split record" at line 3, and my code.

Call the code with

Python pipesmoker.py MyFile.txt ,

(first parameter is the file being read, second parameter is the field 
separator... a comma in this case)

I can read the file in, I can determine that I'm looking for records which have 
13 fields and I can find a record which is too short (line 3).

What I can't do is read the successive line to a short line in order to append 
it onto the end of short line before writing the entire amended line out. I'm 
still thinking about how to persuade the fileinput module to leap over the 
successor line so it doesn't get processed again.

When I run the code as it stands, I get a traceback as I'm obviously not using 
fileinput.FileInput.readline() correctly.

value of file is C:\myfile.txt
value of the delimiter is ,
I'm looking for  13 , in each currentLine...
"1","0000000688      ","ABCD","930020854","34","0","1"," ","930020854 ","    
      ","0","0","0","0"

"2","0000000688      ","ABCD","930020854","99","0","1"," ","930020854 ","       
   ","0","0","0","0"

short line found at line 3
Traceback (most recent call last):
  File "C:\Documents and 
Settings\flynns\workspace\PipeSmoker\src\pipesmoker\pipesmoker.py", line 35, in 
<module>
    nextLine = fileinput.FileInput.readline(args.file)
  File "C:\Python32\lib\fileinput.py", line 301, in readline
    line = self._buffer[self._bufindex]
AttributeError: 'str' object has no attribute '_buffer'


Can someone explain to me how I am supposed to make use of readline() to grab 
the next line of a text file please? It may be that I should be using some 
other module, but chose fileinput as I was hoping to make the little routine as 
generic as possible; able to spot short lines in tab separated, comma 
separated, pipe separated, ^~~^ separated and anything else which my clients 
feel like sending me.

-- 
Steve Flynn


This email and any attachment to it are confidential.  Unless you are the 
intended recipient, you may not use, copy or disclose either the message or any 
information contained in the message. If you are not the intended recipient, 
you should delete this email and notify the sender immediately.

Any views or opinions expressed in this email are those of the sender only, 
unless otherwise stated.  All copyright in any Capita material in this email is 
reserved.

All emails, incoming and outgoing, may be recorded by Capita and monitored for 
legitimate business purposes. 

Capita exclude all liability for any loss or damage arising or resulting from 
the receipt, use or transmission of this email to the fullest extent permitted 
by law.
"1","0000000688      ","ABCD","930020854","34","0","1"," ","930020854 ","    
      ","0","0","0","0"
"2","0000000688      ","ABCD","930020854","99","0","1"," ","930020854 ","       
   ","0","0","0","0"
"3","0000000695      ","ABCD","930610
085","34","0","1"," ","930610085 ","          ","0","0","0","0"
"4","0000000695      ","ABCD","930610085","99","0","1"," ","930610085 ","       
   ","0","0","0","0"
"5","0000000696      ","ABCD","930610086","34","0","1"," ","930610086 ","       
   ","0","0","0","0"
"6","0000000696      ","ABCD","930610086","99","0","1"," ","930610086 ","       
   ","0","0","0","0"
"7","0000000699      ","ABCD","930610087","34","0","1"," ","930610087 ","       
   ","0","0","0","0"
"8","0000000699      ","ABCD","930610087","99","0","1"," ","930610087 ","       
   ","0","0","0","0"
"9","0000000701      ","ABCD","930610088","34","0","1"," ","930610088 ","       
   ","0","0","0","0"
"10","0000000701      ","ABCD","930610088","99","0","1"," ","930610088 ","      
    ","0","0","0","0"

Attachment: pipesmoker.py
Description: pipesmoker.py

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to