RE: Help required to read and print lines based on the type of first character




-----Original Message-----
From: Bruno Desthuilliers [mailto:bruno.42.desthuilliers@xxxxxxxxxxxxxxxxxxx]
Sent: Thursday, March 05, 2009 10:45 PM
To: python-list@xxxxxxxxxx
Subject: Re: Help required to read and print lines based on the type of first character

Abhinayaraj.Raju@xxxxxxxxxx a écrit :

<ot>
Please, don't top-post, and learn to quote & snip
(if you don't know what top-posting is, google is your friend).
</ot>

Thank you so much for your guidance, Bruno.

This should help me in a long way.

Here is the code I have written.

path = raw_input("\nEnter location eg. c:/buffer/test.txt : \n")
fileIN = open(path)

This will break if the file can't be opened (doesn't exist, is
protected, whatever).

count = 0

'count' is used locally in the loop, so it shouldn't be set outside it.

for line in fileIN:
data= line

if '####' in data:
count = 4
elif '###' in data:
count = 3
elif '##' in data:
count = 2
elif '#' in data:
count = 1
elif data.find('#') == -1:

This test is redundant. if the "'#' in data" evals to False, then
data.find('#') is garanteed to return -1.

count = 0


if (count == 0 and data!=""):

the file iterator yields lines with the newline character included, so
there's no way that data == "" (unless you explicitely remove the
newline character yourself). Read doc for the strip method of string
objects.

Also, you don't need the parens.

print data + '\nlooks like a code line...\n'
elif(count== 4):
print data + '\ninvalid line!\n'
elif count>=1:
for i in range(0, count):
print data

You didn't address my questions wrt/ specs clarifications. There's a
*big* difference between *containing* a substring and *starting with* a
substring. Hint: Python strings have a "startswith" method...

Also and FWIW, since Python >= 2.5.2 (and possibly >= 2.5.0 - I let you
check the docs), Python's strings have a count method too.

Your code is not too bad for a beginner - I've done worse when I started
-, but it doesn't really matches the specs (which is impossible FWIW
given the ambiguities they contain, cf my previous post), and contains a
couple of more or less useless or redundant tests which make it looks a
bit like "programming by accident", and doesn't really uses Python's
string handling features.

HTH

Bruno,
Thanks once again.

Yeah, the specification is that it should seek for the first character (sub string) and based on that it should take the decision. So this is "*starting with* a
substring." Case only.

And as far as your review comments are concerned, I need to have a look at that all those doc's you have mentioned. That should help.

-Abhi


.



Relevant Pages

  • Re: Help required to read and print lines based on the type of first character
    ... Please, don't top-post, and learn to quote & snip ... elif '##' in data: ... Your code is not too bad for a beginner - I've done worse when I started -, but it doesn't really matches the specs, and contains a couple of more or less useless or redundant tests which make it looks a bit like "programming by accident", and doesn't really uses Python's string handling features. ...
    (comp.lang.python)
  • Re: Data String Manipulation
    ... Function MakeString(Scan As String) As String ... Dim Ret As String ... GoTo Exit_Func ... always need to delete the first character in the scanned string, ...
    (microsoft.public.access.modulesdaovba)
  • Re: LPCWSTR to CString conversion problem
    ... I get the first character of the string and pass it to ... LPCWSTR to CString conversion. ... Do you compile as Unicode or Ansi? ... GetAtreturns the second character of the string, ...
    (microsoft.public.vc.atl)
  • Re: Converting Type Characters to type string
    ... used to determine whether or not the input was a integer roman numeral ... advice and are using Get_Line to read an entire string first, ... look at the first character of the string to see whether it's Q or a ... can still convert the entire input string using Integer'Value. ...
    (comp.lang.ada)
  • Re: Capitalizing the first letter
    ... Education Technology Department Ankara / TURKEY ... In PHP ucfirstfunction make a string's first character uppercase. ... But ralph smith have two words respectivetely ralph and smith. ... string, ...
    (php.general)