By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,404 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,404 IT Pros & Developers. It's quick & easy.

(python) for some reason file.readline() is only reading every second line

P: 4
why is this skipping readlines in what it returns? i went through debugger and found it thinks the second line of the txt file is the first and continues to skip every second line.
Expand|Select|Wrap|Line Numbers
  1. def get_distances(filename):
  2.     opener = open(filename)
  3.     d = {}
  4.     while file.readline(opener) != '':
  5.         '''converts a file with cities and distances into a dictionary with values
  6.         of distances from one city to other cities and then stores that dictionary
  7.         in a list with values specific to each city
  8.         '''
  9.         #storing the line which has been read in a list
  10.         read = file.readline(opener)
  11.         my_list = read.split(":")
  12.         my_list[1] = my_list[1].split()
  13.  
  14.         max = len(my_list[1]) - 1
  15.         my_list.append(my_list[1][max])
  16.         my_list[1].pop()
  17.  
  18.         if max > 1:
  19.             my_list[1] = my_list[1][0] + ' ' + my_list[1][1]
  20.         else:
  21.             my_list[1] = my_list[1][0]
  22.         print my_list
  23.         if d.has_key(my_list[0]):
  24.             d[my_list[0]].append((my_list[1],my_list[2]))
  25.         else:
  26.             d[my_list[0]] = [(my_list[1],my_list[2])]
  27.     return d
  28. print get_distances('cities.txt')
  29.  
and it returns. i put the print of my_list in there to illustrate how it skips lines.
Expand|Select|Wrap|Line Numbers
  1. ['New York', 'Washington', '2']
  2. ['San Francisco', 'Mexico City', '3']
  3. ['Toronto', 'San Francisco', '6']
  4. {'Toronto': [('San Francisco', '6')], 'San Francisco': [('Mexico City', '3')], 'New York': [('Washington', '2')]}
  5.  
and etc. depending on how many more cities i add to the txt file. any suggestions on how to solve this problem?
my text file is 'cities.txt'
Expand|Select|Wrap|Line Numbers
  1. Toronto:New York 3
  2. New York:Washington 2
  3. Washington:San Francisco 5
  4. San Francisco:Mexico City 3
  5. Toronto:Mexico City 7
  6. Toronto:San Francisco 6
  7.  
Attached Images
File Type: jpg problem.jpg (43.2 KB, 1445 views)
Oct 30 '11 #1

✓ answered by david2031

i realize i the while statement reads it once everytime the loop goes through thus skipping the line. sorry for waste.

Share this Question
Share on Google+
3 Replies


P: 4
i realize i the while statement reads it once everytime the loop goes through thus skipping the line. sorry for waste.
Oct 30 '11 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Instead of using readline(), try iterating on the file object as in:
Expand|Select|Wrap|Line Numbers
  1. f = open("filename.txt")
  2. for line in f:
  3.     .... do stuff ...
  4. f.close()
Oct 31 '11 #3

P: 4
i am just learning python so some obvious things i are not so obvious to me. for that project i ended up making two opens for the same file but as of now i have updated myself on how and when to use for loops. thanks.
Nov 1 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.