473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

read lines

Hi, I have a text file like this;

1 -33.453579
2 -148.487125
3 -195.067172
4 -115.958374
5 -100.597841
6 -121.566441
7 -121.025381
8 -132.103507
9 -108.939327
10 -97.046703
11 -52.866534
12 -48.432623
13 -112.790419
14 -98.516975
15 -98.724436

So I want to write a program in python that reads each line and
detects which numbers of the second column are the maximum and the
minimum.

I tried with;

import os, sys,re,string

# first parameter is the name of the data file
name1 = sys.argv[1]
infile1 = open(name1,"r")

# 1. get minimum and maximum

minimum=0
maximum=0
print " minimum = ",minimum
print " maximum = ",maximum
while 1:
line = infile1.readline()
ll = re.split("\s+",string.strip(line))
print ll[0],ll[1]
a=ll[0]
b=ll[1]
print a,b
if(b<minimum):
minimum=b
print " minimum= ",minimum
if(b>maximum):
maximum=b
print " maximum= ",maximum

print minimum, maximum
But it does not work and I get errors like;

Traceback (most recent call last):
File "translate_to_intervals.py", line 20, in <module>
print ll[0],ll[1]
IndexError: list index out of range
Could anybody help me ?

Thanks

Dec 4 '07 #1
8 2061
On Dec 4, 2:14 pm, Horacius ReX <horacius....@gmail.comwrote:
Hi, I have a text file like this;

1 -33.453579
2 -148.487125
3 -195.067172
4 -115.958374
5 -100.597841
6 -121.566441
7 -121.025381
8 -132.103507
9 -108.939327
10 -97.046703
11 -52.866534
12 -48.432623
13 -112.790419
14 -98.516975
15 -98.724436

So I want to write a program in python that reads each line and
detects which numbers of the second column are the maximum and the
minimum.

I tried with;

import os, sys,re,string

# first parameter is the name of the data file
name1 = sys.argv[1]
infile1 = open(name1,"r")

# 1. get minimum and maximum

minimum=0
maximum=0

print " minimum = ",minimum
print " maximum = ",maximum

while 1:
line = infile1.readline()
ll = re.split("\s+",string.strip(line))
print ll[0],ll[1]
a=ll[0]
b=ll[1]
print a,b
if(b<minimum):
minimum=b
print " minimum= ",minimum
if(b>maximum):
maximum=b
print " maximum= ",maximum

print minimum, maximum

But it does not work and I get errors like;

Traceback (most recent call last):
File "translate_to_intervals.py", line 20, in <module>
print ll[0],ll[1]
IndexError: list index out of range

Could anybody help me ?

Thanks
You're not guaranteed to have that 2 or even 1 element after
splitting. If the line is empty or has 1 space you need to handle
it. Also is there really a need for regex for a simple string split ?

import sys

infile = open(sys.argv[1], 'r')
min, max = 0, 0

for each_line in infile.readlines():
if each_line.strip():
tmp = each_line.strip().split()
try:
b = tmp[1]
except IndexError:
continue
if b < min: min = b
if b max: max = b
Dec 4 '07 #2
Hi, I have a text file like this;
>
1 -33.453579
2 -148.487125
....

So I want to write a program in python that reads each line and
detects which numbers of the second column are the maximum and the
minimum.

I tried with;

import os, sys,re,string

# first parameter is the name of the data file
name1 = sys.argv[1]
infile1 = open(name1,"r")

# 1. get minimum and maximum

minimum=0
maximum=0
print " minimum = ",minimum
print " maximum = ",maximum
while 1:
line = infile1.readline()
ll = re.split("\s+",string.strip(line))
print ll[0],ll[1]
a=ll[0]
b=ll[1]
print a,b
if(b<minimum):
minimum=b
print " minimum= ",minimum
if(b>maximum):
maximum=b
print " maximum= ",maximum

print minimum, maximum
But it does not work and I get errors like;

Traceback (most recent call last):
File "translate_to_intervals.py", line 20, in <module>
print ll[0],ll[1]
IndexError: list index out of range
Your regex is not working correctly I guess, I don't even know why you are
using a regex, something like this would work just fine:

import sys
nums = [float(line.split(' -')[1]) for line in open(sys.argv[1])]
print 'min=', min(nums), 'max=', max(nums)
Dec 4 '07 #3
On 2007-12-04, Horacius ReX <ho**********@gmail.comwrote:
Hi, I have a text file like this;

1 -33.453579
2 -148.487125
3 -195.067172
4 -115.958374
5 -100.597841
6 -121.566441
7 -121.025381
8 -132.103507
9 -108.939327
10 -97.046703
11 -52.866534
12 -48.432623
13 -112.790419
14 -98.516975
15 -98.724436

So I want to write a program in python that reads each line and
detects which numbers of the second column are the maximum and
the minimum.
Check out 3.6.1 String Methods in the Python Library Reference.
It contains what you need.

Also, read about max and min from 2.1 Built-in Functions.
I tried with;

import os, sys,re,string
The string module is best avoided, except for a few character
classes, e.g., Paladins and Clerics. ;-) Use str methods instead.

It's more readable to import one module per line.
# first parameter is the name of the data file
name1 = sys.argv[1]
infile1 = open(name1,"r")

# 1. get minimum and maximum

minimum=0
maximum=0
print " minimum = ",minimum
print " maximum = ",maximum
while 1:
line = infile1.readline()
This isn't the best way to read files in Python. Check out 7.2
Reading and Writing Files in the Python Tutorial.
ll = re.split("\s+",string.strip(line))
print ll[0],ll[1]
a=ll[0]
b=ll[1]
Don't mix tabs and spaces. Python's Style Guide generally
recommends four spaces per indent.
print a,b
if(b<minimum):
readline returns str objects. You'll need to convert them to
numbers manually before comparing.
minimum=b
print " minimum= ",minimum
if(b>maximum):
maximum=b
print " maximum= ",maximum

print minimum, maximum
But it does not work and I get errors like;

Traceback (most recent call last):
File "translate_to_intervals.py", line 20, in <module>
print ll[0],ll[1]
IndexError: list index out of range
This is caused by line becoming an empty string when readline
encounters end of the file.
Could anybody help me ?
The following will not work in Python 2.4 or earlier.

from __future__ import with_statement
import sys
from operator import itemgetter
from contextmanager import closing

with closing(file(sys.argv[1])) as fp:
table = [(int(i), float(n)) for i, n in (line.split() for line in fp)]
print table
print "maximum =", max(table, key=itemgetter(1))
print "minimum =", min(table, key=itemgetter(1))

--
Neil Cerutti
Dec 4 '07 #4
>>>>Horacius ReX <ho**********@gmail.com(HR) wrote:
>HRwhile 1:
HR line = infile1.readline()
You have an infinite loop. Fortunately your program stops because of the
error. When you encounter end of file, line becomes the empty string and
the split gives you only 1 item instead of 2.

So add the following:
if not line: break

Also your choice for 0 as initial values of minimum and maximum isn't good.

--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Dec 4 '07 #5
Your regex is not working correctly I guess, I don't even know why you
are using a regex, something like this would work just fine:

import sys
nums = [float(line.split(' -')[1]) for line in open(sys.argv[1])]
print 'min=', min(nums), 'max=', max(nums)
Sorry, that should be line.split() - didn't realise those were negative
numbers.
Dec 4 '07 #6
Bruno Desthuilliers a écrit :
(snip)
# Notice that here, b is a string, not a number...
try:
b = int(tmp[1])
oops, I meant:
b = float(tmp[1])
Idem here:
def extract_number(iterable):
for linenum, line in enumerate(iterable):
try:
yield int(line.strip().split()[1])
yield float(line.strip().split()[1])

Dec 4 '07 #7
Bruno Desthuilliers wrote:
# You don't need to read the whole file in memory
lines1, lines2 = tee(infile)
print min(extract_numbers(lines1)), max(extract_numbers(lines2))
tee() internally maintains a list of items that were seen by
one but not all of the iterators returned. Therefore after calling min()
and before calling max() you have a list of one float per line in memory
which is quite close conceptually to reading the whole file in memory.

If you want to use memory efficiently, stick with the for-loop.

Peter
Dec 4 '07 #8
Peter Otten a écrit :
Bruno Desthuilliers wrote:
># You don't need to read the whole file in memory
>lines1, lines2 = tee(infile)
print min(extract_numbers(lines1)), max(extract_numbers(lines2))

tee() internally maintains a list of items that were seen by
one but not all of the iterators returned. Therefore after calling min()
and before calling max() you have a list of one float per line in memory
which is quite close conceptually to reading the whole file in memory.

If you want to use memory efficiently, stick with the for-loop.
Indeed - I should have specified that the second version was not
necesseraly better wrt/ either perfs and/or resources usage. Thanks for
having made this point clear.

Dec 4 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
3
by: Jeremy | last post by:
I have a most aggravating problem. I don't understand what is causing readlines() not to read all the lines in the file. I have the following syntax: # some initial stuff XS =...
2
by: VM | last post by:
Is it possible to open an ascii file, read the first 499 lines, overwrite line 500 with the new string, and close the file? All the lines before and after line 500 will be untouched. Thanks.
3
by: JenHu | last post by:
Hi, I want read line by line and characters. The characters are fix length text file, no specific delimited method between each fields. The first line is header line, the last line is footer. ...
24
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
1
by: NeoGregorian | last post by:
Hello, I am writing a wrapper to a basic Input/Output programs (where you type a one line command at a time and then get 0 or more lines of output before you can input the next command). I'm...
6
by: bmerlover | last post by:
I'm trying to read each line, character by character, to determine if a line has a semicolon in it. I'm trying to count the number of lines that have a semicolon. In all of the files in one...
28
by: tlpell | last post by:
Hey, read some tips/pointers on PHP.net but can't seem to solve this problem. I have a php page that reads the contents of a file and then displays the last XX lines of the file. Problem is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.