You are not logged in.

#1 2015-08-07 16:38:57

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

python easy problem [SOLVED]

I just start python, so the reason of my topic. My problem today is about the fact that I cannot read odd but i can read even.

fh = open("bd1.txt", "r")

print ("Name of the file: ", fh.name)       

for index in range(8):
    line = next(fh)
    line = next(fh)
    print ("Line No %d - %s" % (index, line))

# Close opend file
fh.close()

* This one work but for the other code, i don't have much to show than what you see from the present code.  Because I have a hard time to make me understand I will provide you with what I got so far.

fh = open("bd1.txt", "r")

print ("Name of the file: ", fh.name)       

for index in range(0,8,2):
    line = fh.readline()
    print ("Line No %d - %s" % (index, line))

# Close opend file
fh.close()

Feel free to help me otherwise I appreciate all your effort.

Last edited by j2lapoin (2015-08-07 19:01:56)

Offline

#2 2015-08-07 16:51:10

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,789

Re: python easy problem [SOLVED]

Moving to Programming and Scripting


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2015-08-07 17:01:27

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,789

Re: python easy problem [SOLVED]

I do not understand exactly what you are trying to do, but let me try.

The first program reads 16 lines from a file, prints out 8 lines with line numbered 0 through 7, along with the contents of every other line. (The even ones).  In that case, the loop executes 8 times with index taking on the values 0,1,2,3,4,5,6,7.
The second program reads 4 lines from a file, prints out 4 lines numbered 0,2,4,6 fillowed by the contents of lines 1,2,3,and 4.  In the second program, the loop executes four times, reading one line from the file each time.  Index takes on the values 0,2,4,6.

Are you trying to write a program that prints the odd lines?

Also, I am obliged to ask -- Is this a homework problem?  If so, that is fine.  Just let us know so we don't just tell you the answer, but rather will guide you there.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#4 2015-08-07 17:20:14

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: python easy problem [SOLVED]

ewaller wrote:

I do not understand exactly what you are trying to do, but let me try.

The first program reads 16 lines from a file, prints out 8 lines with line numbered 0 through 7, along with the contents of every other line. (The even ones).  In that case, the loop executes 8 times with index taking on the values 0,1,2,3,4,5,6,7.
The second program reads 4 lines from a file, prints out 4 lines numbered 0,2,4,6 fillowed by the contents of lines 1,2,3,and 4.  In the second program, the loop executes four times, reading one line from the file each time.  Index takes on the values 0,2,4,6.

Are you trying to write a program that prints the odd lines?

Also, I am obliged to ask -- Is this a homework problem?  If so, that is fine.  Just let us know so we don't just tell you the answer, but rather will guide you there.

No, it's not a homework but I appreciate learning so it will be fine with me. I'm effectively trying to write the program so it can print the odd lines.  The first part or code, is the one that work but the second part is missing something.  for output, eg...
bd1.txt:

core/acl 2.2.52-2 [installed]
    Access control list utilities, libraries and headers
core/archlinux-keyring 20150605-1 [installed]
    Arch Linux PGP keyring
core/attr 2.4.47-1 [installed]
    Extended attribute support library for ACL support
core/autoconf 2.69-2 (base-devel) [installed]
    A GNU tool for automatically configuring source code
core/automake 1.15-1 (base-devel) [installed]
    A GNU tool for automatically creating Makefiles
core/b43-fwcutter 019-1
    firmware extractor for the b43 kernel module
core/bash 4.3.039-1 (base) [installed]
    The GNU Bourne Again shell
core/binutils 2.25.1-1 (base-devel) [installed]
    A set of programs to assemble and manipulate binary and object files

the part i succeed...

# Open a file
fh = open("bd1.txt", "r")

print ("Name of the file: ", fh.name)       

for index in range(8):
    line = next(fh)
    line = next(fh)
    print ("Line No %d - %s" % (index, line))

# Close opend file
fh.close()
Name of the file:  bd1.txt
Line No 0 -     Access control list utilities, libraries and headers

Line No 1 -     Arch Linux PGP keyring

Line No 2 -     Extended attribute support library for ACL support

Line No 3 -     A GNU tool for automatically configuring source code

Line No 4 -     A GNU tool for automatically creating Makefiles

Line No 5 -     firmware extractor for the b43 kernel module

Line No 6 -     The GNU Bourne Again shell

Line No 7 -     A set of programs to assemble and manipulate binary and object files

I want to print the other part.

Offline

#5 2015-08-07 17:24:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,529
Website

Re: python easy problem [SOLVED]

So rather than taking a completely different approach use a loop just like the one that works.  Just think about when you want to print the contents of the line variable.

For even numbers, your read two lines, then print the last one.  To get the odd lines, you still need to read two lines every time through the loop, you just want to print the first of the two.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2015-08-07 19:00:25

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: python easy problem [SOLVED]

Awesome.

(solution)

h = open("bd1.txt", "r")

print ("Name of the file: ", fh.name)       

for index in range(8):
    line = next(fh)
    print ("Line No %d - %s" % (index, line))
    line = next(fh)

# Close opend file
fh.close()

Thanks for the last comment, It help me thru this challenge.

Offline

#7 2015-08-07 19:10:48

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: python easy problem [SOLVED]

You should generally use with when opening files as described here.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#8 2015-08-07 19:27:43

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: python easy problem [SOLVED]

i'm working on it. this conversion is hard.

Offline

#9 2015-08-07 19:37:00

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: python easy problem [SOLVED]

hope you appreciate a better code...let me know if there is more missing to my attention.

# Open a file
with open("bd1.txt", "r") as fh:       

    for index in range(8):
        line = next(fh)
        line = next(fh)
        print ("Line No %d - %s" % (index, line))

# Close opend file
fh.close()
with open("bd1.txt", "r") as fh:      

    for index in range(3):
        line = next(fh)
        print ("Line No %d - %s" % (index, line))
        line = next(fh) 

    content=fh.readlines()

    for lines in content:

        words=lines
    
    #if (words[-12:] == "[installed]\n"):
print("[*]")
#else:
print ("[_]")

# Close opend file
fh.close()

Offline

#10 2015-08-07 20:52:36

Badrocklo
Member
Registered: 2013-02-20
Posts: 13

Re: python easy problem [SOLVED]

No need to close the file at the end when you use the with statement, it's automatically handled.

Offline

#11 2015-08-07 21:33:16

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: python easy problem [SOLVED]

Is the 'pacman -Qs' output in your post just an example, or are you trying to specifically parse the output of pacman?


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#12 2015-08-07 22:49:07

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: python easy problem [SOLVED]

I'm trying to made a frontend in python for pacman. just by pleasure. I did one in bash but python seem way harder.

Offline

Board footer

Powered by FluxBB