| | |
Is there a clever way to do this?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2009
Posts: 70
Reputation:
Solved Threads: 2
Hey guys,
I'm new to python so if this is a silly question, pardon me.
I have the following basic assignment:
Take a user-specified range of lines from some data file, call it input, and write them to an output data file. What I want to do is have the user specify the range (for example lines 10-20) that will be picked from the input file to the output file. I am trying to use readlines() and I am able to get the program to pick a certain number of lines, but it always begins as line 1. For example, if I specify lines (30-200) it will recognize that it must extract 170 lines from the input file; however, it starts at line 1 and runs to line 170 instead of starting at line 30. Here is a snipet of the code:
first = int(raw_input('Enter a starting value'))
last = int(raw_input('Enter a final value'))
def add_line_numbers(infile, outfile):
f = open(infile,'r')
o = open(outfile,'w')
i = 1
for i in range(first, last):
o.write(str(i)+'\t'+f.readline())
f.close()
o.close()
--- Parsing code follows
The code originally worked fine until I began editing it. The original code takes an entire input file and transfers it to an entire output file. So only my edits are in question.
The 'i' acts as a counter, and that part works fine. The counter will read 30-200 for example; however, the lines that are inputing are still 1-170 from the original data type.
As I said, I am new to python and very amenable to suggestions so if you have a smarter way to tackled this problem, I am all ears.
This has been a very frustrating process. I'd appreciate help so much.
I'm new to python so if this is a silly question, pardon me.
I have the following basic assignment:
Take a user-specified range of lines from some data file, call it input, and write them to an output data file. What I want to do is have the user specify the range (for example lines 10-20) that will be picked from the input file to the output file. I am trying to use readlines() and I am able to get the program to pick a certain number of lines, but it always begins as line 1. For example, if I specify lines (30-200) it will recognize that it must extract 170 lines from the input file; however, it starts at line 1 and runs to line 170 instead of starting at line 30. Here is a snipet of the code:
first = int(raw_input('Enter a starting value'))
last = int(raw_input('Enter a final value'))
def add_line_numbers(infile, outfile):
f = open(infile,'r')
o = open(outfile,'w')
i = 1
for i in range(first, last):
o.write(str(i)+'\t'+f.readline())
f.close()
o.close()
--- Parsing code follows
The code originally worked fine until I began editing it. The original code takes an entire input file and transfers it to an entire output file. So only my edits are in question.
The 'i' acts as a counter, and that part works fine. The counter will read 30-200 for example; however, the lines that are inputing are still 1-170 from the original data type.
As I said, I am new to python and very amenable to suggestions so if you have a smarter way to tackled this problem, I am all ears.
This has been a very frustrating process. I'd appreciate help so much.
•
•
Join Date: Dec 2006
Posts: 1,028
Reputation:
Solved Threads: 290
You read it one line at a time, but write only if the counter is between the numbers. And don't use "i", "l", or "o" as single variables because they look too much like numbers.
Python Syntax (Toggle Plain Text)
f = open(infile,'r') o = open(outfile,'w') ctr = 1 for line in f: if first <= ctr <= last: o.write(str(ctr)+'\t'+line) ctr += 1 f.close() o.close()
Last edited by woooee; May 20th, 2009 at 9:50 pm.
•
•
Join Date: May 2009
Posts: 25
Reputation:
Solved Threads: 8
There's always a more clever way :-)
Python Syntax (Toggle Plain Text)
start = 10 end = 20 open(outfile, 'w').writelines(open(infile).readlines()[start:end])
![]() |
Similar Threads
- Word Association Game (Posting Games)
- accessing private data members (C++)
- how to upload Word doc with columns & graphics? (Graphics and Multimedia)
- I can’t get into windows 98!!!!!! Can anyone HELP (Windows 95 / 98 / Me)
- loging in to samba from a xp box (*nix Software)
- so smart so dumb (Geeks' Lounge)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the Python Forum
- Previous Thread: Recommend post Python 3 books
- Next Thread: code for get the database values into the dropdown box
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang slicenotation split string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web windows wx.wizard wxpython xlwt






