python - Creating a list from a file -


i'm creating horoscope have file different qualities , 4-5 sentences different statements regarding each quality. qaulities separated blank line. want save them list called qualities, qualities[0] contains sentences regaring first quality, qualities[1] contains sentences regarding second , on.

my code:

class horoscope:      def __init__(self, filename):         self.qualities = list()         file = open(filename, 'ru')         = 0         line in file:             row= line.strip().split('/')             if len(row) >= 2:                 self.qualities[0+i] = row             else:                 += 1         file.close() filename= 'horoscope.txt'  horoscope= horoscope(filename)  print(horoscope.qualities) 

unfortunally, printed "[ ]"... know why? thanks!

i'm surprised self.qualities[i] did not raise indexerror. suggests len(row) never >= 2. however, if were, should use append instead:

class horoscope:     def __init__(self, filename):         self.qualities = list()         open(filename, 'ru') f:             line in f:                 row = line.strip().split('/')                 if len(row) >= 2:                     self.qualities.append(row)  filename= 'horoscope.txt' horoscope= horoscope(filename) print(horoscope.qualities) 

note not replicate logic of original code. appends every row len(row) >= 2. original code more complicated, overwriting @ same index, advancing index. want that? if so, want place in list @ locations index advanced? none? value have placed @ every index... (you can't have list values at, say, second , fifth index. list of length 5 has have 5 values.)


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -