473,657 Members | 2,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

readlines()

Hi,
I use readlines() to read one data file. Python automatically parses the read contents
into a list of lines. When I used list[0] to print out the 1st line, it is ok. When I use
the list index 2 to print out the 2nd line , there is an error mesage. I only need one line of
input data file in the middle of the file. For example, I have data file like:
---------------------------------------------------------------------------
Timestamp: Sat Aug 7 11:14:57 AM
Adapter Address: 00:60:08:2A:C9: 5A
IP Address: 165.91.10.244
Directory ID: 0675392c736079c fd81a55028df3cb 43
Domain Name: bdanwood.dsl.ta mu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------
Timestamp: Sat Aug 7 11:15:56 PM
Adapter Address: 00:60:08:2A:C9: 5A
IP Address: 165.91.10.244
Directory ID: 0675392c736079c fd81a55028df3cb 43
Domain Name: bdanwood.dsl.ta mu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------

I have some codes:
.......
for line in db1:
(ip, mac) = string.split(li ne)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data = getdata.readlin es()
print 'data[0] is', data[0]
print 'data[3] is', data[3]
getdata.close()

When run the codes, I got:
data[0] is ---------------------------------------------------------------------------

data[3] is
Traceback (innermost last):
File "com1", line 64, in ?
print 'data[3] is', data[3]
IndexError: list index out of range

How can I fix it ?

Thanks,
Jul 18 '05 #1
2 6202
md
I am not sure which name is designating your source text (db1 or getdata
?). If it is 'getdata' then you will need to iterate over the file
object to grab all of the lines. Try this:

for line in db1:
(ip, mac) = string.split(li ne)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data=[]
for line in getdata.readlin es():
data.append(lin e)
print 'data[0] is', data[0]
print 'data[3] is', data[3]
getdata.close()

Hope this helps,
Mark d.
Yong Wang wrote:
Hi,
I use readlines() to read one data file. Python automatically parses the read contents
into a list of lines. When I used list[0] to print out the 1st line, it is ok. When I use
the list index 2 to print out the 2nd line , there is an error mesage. I only need one line of
input data file in the middle of the file. For example, I have data file like:
---------------------------------------------------------------------------
Timestamp: Sat Aug 7 11:14:57 AM
Adapter Address: 00:60:08:2A:C9: 5A
IP Address: 165.91.10.244
Directory ID: 0675392c736079c fd81a55028df3cb 43
Domain Name: bdanwood.dsl.ta mu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------
Timestamp: Sat Aug 7 11:15:56 PM
Adapter Address: 00:60:08:2A:C9: 5A
IP Address: 165.91.10.244
Directory ID: 0675392c736079c fd81a55028df3cb 43
Domain Name: bdanwood.dsl.ta mu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------

I have some codes:
.......
for line in db1:
(ip, mac) = string.split(li ne)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data = getdata.readlin es()
print 'data[0] is', data[0]
print 'data[3] is', data[3]
getdata.close()

When run the codes, I got:
data[0] is ---------------------------------------------------------------------------

data[3] is
Traceback (innermost last):
File "com1", line 64, in ?
print 'data[3] is', data[3]
IndexError: list index out of range

How can I fix it ?

Thanks,

Jul 18 '05 #2
Yong Wang wrote:
Hi,
I use readlines() to read one data file. Python automatically parses the read contents
into a list of lines. When I used list[0] to print out the 1st line, it is ok. When I use
the list index 2 to print out the 2nd line , there is an error mesage. I only need one line of
input data file in the middle of the file. For example, I have data file like:
---------------------------------------------------------------------------
Timestamp: Sat Aug 7 11:14:57 AM
Adapter Address: 00:60:08:2A:C9: 5A
IP Address: 165.91.10.244
Directory ID: 0675392c736079c fd81a55028df3cb 43
Domain Name: bdanwood.dsl.ta mu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------
Timestamp: Sat Aug 7 11:15:56 PM
Adapter Address: 00:60:08:2A:C9: 5A
IP Address: 165.91.10.244
Directory ID: 0675392c736079c fd81a55028df3cb 43
Domain Name: bdanwood.dsl.ta mu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------

I have some codes:
.......
for line in db1:
(ip, mac) = string.split(li ne)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data = getdata.readlin es()
print 'data[0] is', data[0]
print 'data[3] is', data[3]
getdata.close()

When run the codes, I got:
data[0] is ---------------------------------------------------------------------------

data[3] is
Traceback (innermost last):
File "com1", line 64, in ?
print 'data[3] is', data[3]
IndexError: list index out of range

How can I fix it ?

Thanks,

Yong,
In general, for debugging, why not use

for i in range(len(data) ):
print 'data[',i,'] is',data[i]

wes

Jul 18 '05 #3

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

Similar topics

3
11522
by: Leeds, Mark | last post by:
I did more investigation into my previous problem and what happens is that my text file has \r for representing a new line instead of a \n. is there a way to tell the readlines function that the symbol for a newline is \r rather than \n ? right now the readlines function reads the\r as another item in the list and just puts everything into one big list because it doesn't find any new line characters. thanks. i'm really stuck because
2
2192
by: Jorge Godoy | last post by:
Hi! I'm trying to get a specific information from inside an image and it works correctly on unices. What I do is: 1. associate a filehandle with the image file 2. get the desired line 3. read the amount of data I want 4. close the filehandle
9
29220
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get the error message:
2
4147
by: vch | last post by:
Does a call to file.readlines() reads all lines at once in the memory? Are the any reasons, from the performance point of view, to prefer *while* loop with readline() to *for* loop with readlines()?
34
2798
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what happens if the number of lines is huge? I have a very big file (4GB) I want to read in, but I'm sure there must be some limitation to readlines and I'd like to know how it is handled by python. I am using it like this:
4
1463
by: wscrsurfdude | last post by:
Ik have an uml file I want to read with readlines. I have the following code: infile = open("out2.txt","r") for line in infile.readlines(): print line The print statement just gives the data, not the uml headings. Is there a solution which also gives the uml headings.
7
4099
by: Wojciech Gryc | last post by:
Hi, I'm currently using Python to deal with a fairly large text file (800 MB), which I know has about 85,000 lines of text. I can confirm this because (1) I built the file myself, and (2) running a basic Java program to count lines yields a number in that range. However, when I use Python's various methods -- readline(), readlines(), or xreadlines() and loop through the lines of the file, the line program exits at 16,000 lines. No...
3
1098
by: techyvibe | last post by:
i seem to have a problem with readlines()as below i have global f f=open("hello.txt", "r") f1 = open("C:\\log1.txt",'w') f2 = open("C:\\log2.txt",'w')
5
6248
by: zxo102 | last post by:
Hello All, I have a system. An instrument attched to 'com1' is wireless connected to many sensors at different locations. The instrument can forward the "commands" (from pyserial's write()) to those sensors. Based on the "commands", the sensors keep sending corresponding data back to the instrument which wraps up those data and put into "com1" . The readlines() of pyserial pick up those data for processing. The data ’string' does...
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.