You are not logged in.
Pages: 1
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
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
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
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
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
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
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
i'm working on it. this conversion is hard.
Offline
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
No need to close the file at the end when you use the with statement, it's automatically handled.
Offline
Is the 'pacman -Qs' output in your post just an example, or are you trying to specifically parse the output of pacman?
Offline
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
Pages: 1