Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Help required to read and print lines based on the type of firstcharacter

Reply
Thread Tools

Re: Help required to read and print lines based on the type of firstcharacter

 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-04-2009
En Wed, 04 Mar 2009 07:36:01 -0200, <(E-Mail Removed)> escribió:

> I am a beginner in Python. In fact, beginner to coding/ scripting.
>
> Here is a scenario, I need to code. Need your help on this:
>
> A script that
> 1. Reads from a file (may be a local file say test.txt)
> 2. And, if the line begins with a "#", should print the line one
> time
> 3. if the line has "##", should print the line 2 times,
> 4. And, if there are "###" in the beginning of the line, should
> print the same line 3times,
> 5. And, if the line contains "####" or more "#"'s, then print as
> "an invalid line"
> 6. if the line contains no "#" then print "looks like a code line"
>
> For this as far the info I could understand,
>
> - We need to use open('TextFile.txt') function
> - Need to start a While loop/ For in loop
> - By some means individually read every single line and check for
> the above conditions


Start following the tutorial at http://docs.python.org/tut

You'll see it menctions a few ways to process a file; the cleanest
alternative to do it one line at a time is:
for line in some_file:
# do something with line

The interactive interpreter is great to experiment:

py> x = "some text"
py> x.startswith("#")
False
py> y = "## another text"
py> y.startswith("#")
True

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-05-2009
(answering to the OP)
> En Wed, 04 Mar 2009 07:36:01 -0200, <(E-Mail Removed)> escribió:
>
>> I am a beginner in Python. In fact, beginner to coding/ scripting.
>>
>> Here is a scenario, I need to code. Need your help on this:


Your first task here should be to refine the specs - too much
ambiguities in it:

>> A script that
>> 1. Reads from a file (may be a local file say test.txt)


reads what ? The whole content ? A (yet unspecified) portion ? etc...

>> 2. And, if the line


which line ?

>> begins with a "#", should print the line one
>> time


"#### yadda" begins with a "#". So according to this rule, it should be
printed once.

>> 3. if the line has "##", should print the line 2 times,


"#### yadda" 'has' (contains) "##", so it should be printed twice. But
it also _begins_ with a '#', so according to rule 2, it should be
printed once. Is the 'one time' in rule 2 supposed to mean 'at least
onces', or 'once and only once' ? In this last case, rule 3 contradicts
rule 2.

Also, "yadday ## woops" 'has' (contains) "##". According to rule 3, it
should be printed twice. Is that right ?

>> 4. And, if there are "###" in the beginning of the line, should
>> print the same line 3times,


"#### yadda" begins with "###", so it should be printed thrice. It also
contains "##" (cf rule 3) and starts with "#" (cf rule 2). How is this
rule supposed to be understood ?

>> 5. And, if the line contains "####" or more "#"'s, then print as
>> "an invalid line"


"#### yadda" starts with '#', contains '##', and starts with '###'.
Which rule is supposed to apply here ?

>> 6. if the line contains no "#" then print "looks like a code line"
>>


The first step in programming is to get accurate, unambigous and
well-expressed specs. In the above case (I mean, at this level of
detail), "accurate, unambigous and well-expressed specs" are almost
pseudocode. Doing a bit of mind-reading (which is certainly *not* the
right thing to do - in real life, I'd just go back to the customer or
whoever handed me such specs to sort this out), I came out with the
following rewrite:

1. open a given file in text mode
2. read it line by line
3. for each line:
3.1. if the line contains/startswith (?) more than three '#':
print "an invalid line"
3.2. if the line starts with one, two or three '#':
print as many times the line as there are '#'
3.2 else (imply : the line contains no '#'):
print "looks like a code line"

So basically, you have your algorithm. Now you just need to find out how
to do each of these tasks in Python. That is :

a. how to open a file for reading in text mode (NB: 'text mode' may or
not makes sense, according to the OS)

b. how to iterate over the lines in this file once it's correctly opened

c. how to test for the presence and position of a given character /
substring in a string

d. how to "print" something from your program.


Good news: all this is pretty well documented. I'd say that the most
"tricky" part is c., since there's more than one possible solution, but
since it's about strings, looking for what features Python strings has
to offer, and trying them out in the interactive interpreter should
solve the problem very quickly.


HTH
 
Reply With Quote
 
 
 
 
Abhinayaraj.Raju@Emulex.Com
Guest
Posts: n/a
 
      03-05-2009
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)
count = 0
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:
count = 0


if (count == 0 and data!=""):
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
Thanks
Abhi




-----Original Message-----
From: Bruno Desthuilliers [(E-Mail Removed)]
Sent: Thursday, March 05, 2009 6:50 PM
To: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: Re: Help required to read and print lines based on the type of first character

(answering to the OP)
> En Wed, 04 Mar 2009 07:36:01 -0200, <(E-Mail Removed)> escribió:
>
>> I am a beginner in Python. In fact, beginner to coding/ scripting.
>>
>> Here is a scenario, I need to code. Need your help on this:


Your first task here should be to refine the specs - too much
ambiguities in it:

>> A script that
>> 1. Reads from a file (may be a local file say test.txt)


reads what ? The whole content ? A (yet unspecified) portion ? etc...

>> 2. And, if the line


which line ?

>> begins with a "#", should print the line one
>> time


"#### yadda" begins with a "#". So according to this rule, it should be
printed once.

>> 3. if the line has "##", should print the line 2 times,


"#### yadda" 'has' (contains) "##", so it should be printed twice. But
it also _begins_ with a '#', so according to rule 2, it should be
printed once. Is the 'one time' in rule 2 supposed to mean 'at least
onces', or 'once and only once' ? In this last case, rule 3 contradicts
rule 2.

Also, "yadday ## woops" 'has' (contains) "##". According to rule 3, it
should be printed twice. Is that right ?

>> 4. And, if there are "###" in the beginning of the line, should
>> print the same line 3times,


"#### yadda" begins with "###", so it should be printed thrice. It also
contains "##" (cf rule 3) and starts with "#" (cf rule 2). How is this
rule supposed to be understood ?

>> 5. And, if the line contains "####" or more "#"'s, then print as
>> "an invalid line"


"#### yadda" starts with '#', contains '##', and starts with '###'.
Which rule is supposed to apply here ?

>> 6. if the line contains no "#" then print "looks like a code line"
>>


The first step in programming is to get accurate, unambigous and
well-expressed specs. In the above case (I mean, at this level of
detail), "accurate, unambigous and well-expressed specs" are almost
pseudocode. Doing a bit of mind-reading (which is certainly *not* the
right thing to do - in real life, I'd just go back to the customer or
whoever handed me such specs to sort this out), I came out with the
following rewrite:

1. open a given file in text mode
2. read it line by line
3. for each line:
3.1. if the line contains/startswith (?) more than three '#':
print "an invalid line"
3.2. if the line starts with one, two or three '#':
print as many times the line as there are '#'
3.2 else (imply : the line contains no '#'):
print "looks like a code line"

So basically, you have your algorithm. Now you just need to find out how
to do each of these tasks in Python. That is :

a. how to open a file for reading in text mode (NB: 'text mode' may or
not makes sense, according to the OS)

b. how to iterate over the lines in this file once it's correctly opened

c. how to test for the presence and position of a given character /
substring in a string

d. how to "print" something from your program.


Good news: all this is pretty well documented. I'd say that the most
"tricky" part is c., since there's more than one possible solution, but
since it's about strings, looking for what features Python strings has
to offer, and trying them out in the interactive interpreter should
solve the problem very quickly.


HTH

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-05-2009
(E-Mail Removed) 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

 
Reply With Quote
 
Abhinayaraj.Raju@Emulex.Com
Guest
Posts: n/a
 
      03-06-2009

-----Original Message-----
From: Bruno Desthuilliers [(E-Mail Removed)]
Sent: Thursday, March 05, 2009 10:45 PM
To: (E-Mail Removed)
Subject: Re: Help required to read and print lines based on the type of first character

(E-Mail Removed) 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


 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-06-2009
(E-Mail Removed) a écrit :
(snip)

> I need to have a look at that all those doc's you have mentioned. That should help.
>


+1 QOTW !-)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Why does "while (<>) { $x .= $_; } print $x . "\n";" print all input lines? Wolfgang Perl Misc 1 02-13-2004 07:51 PM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments