473,382 Members | 1,647 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,382 software developers and data experts.

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: 0675392c736079cfd81a55028df3cb43
Domain Name: bdanwood.dsl.tamu.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: 0675392c736079cfd81a55028df3cb43
Domain Name: bdanwood.dsl.tamu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------

I have some codes:
.......
for line in db1:
(ip, mac) = string.split(line)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data = getdata.readlines()
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 6186
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(line)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data=[]
for line in getdata.readlines():
data.append(line)
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: 0675392c736079cfd81a55028df3cb43
Domain Name: bdanwood.dsl.tamu.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: 0675392c736079cfd81a55028df3cb43
Domain Name: bdanwood.dsl.tamu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------

I have some codes:
.......
for line in db1:
(ip, mac) = string.split(line)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data = getdata.readlines()
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: 0675392c736079cfd81a55028df3cb43
Domain Name: bdanwood.dsl.tamu.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: 0675392c736079cfd81a55028df3cb43
Domain Name: bdanwood.dsl.tamu.edu
DHCP/NIM Action: lease renewed
Comments:
---------------------------------------------------------------------------

I have some codes:
.......
for line in db1:
(ip, mac) = string.split(line)
print 'ip is ', ip
run = 'dhcpacct --ip=%s > tt1'
os.system(run)
getdata = open('tt1', 'r')
data = getdata.readlines()
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
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...
2
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....
9
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...
2
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
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...
4
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,...
7
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)...
3
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
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...
1
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.