#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Python only reading the last line into my precious list.


    This is a method in my class.

    Code:
    def fraga_efter_fil(self):
                   fil = input("Enter the file") 
                   f = open(fil, "r") 
                   q = f.readlines() 
                   for w in q: 
                        word1 = w.split() 
                        self.listan_med_orden = word1 
                   print (self.listan_med_orden)
    The file i've tried to read into python looks like this (imagine notepad)

    The print at the end is only me checking what get stored in the list, and its at this moment only the words of line 3

    "During the last ten years, the decrease of interest in reptilian
    mammals (line 1)
    has been barely noticeable. We hope that the desire for knowledge (line 2)
    on these strange creatures will continue to exceed our expectations.(line 3)

    So when i try to put all the words into my list with my code, only the words of line 3 appear as elements. Why is that so? Copy the code and try it if you wish.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    You repeated change the value of self.listan_med_orden
    Try this:
    Code:
    def fraga_efter_fil(self):
       fil = input("Enter the file") 
       f = open(fil, "r") 
       q = f.readlines() 
       self.listan_med_orden = []
       for w in q: 
            word1 = w.split() 
            self.listan_med_orden.append(word1)
       print (self.listan_med_orden)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You repeated change the value of self.listan_med_orden
    Try this:
    Code:
    def fraga_efter_fil(self):
       fil = input("Enter the file") 
       f = open(fil, "r") 
       q = f.readlines() 
       self.listan_med_orden = []
       for w in q: 
            word1 = w.split() 
            self.listan_med_orden.append(word1)
       print (self.listan_med_orden)
    Ah yea thats true, but then i get a list with 3 lists in it: Like this:
    [['During', 'the', 'last', 'ten', 'years,', 'the', 'decrease', 'of', 'interest', 'in', 'reptilian'], ['mammals', 'has', 'been', 'barely', 'noticeable.', 'We', 'hope', 'that', 'the', 'desire', 'for', 'knowledge'], ['on', 'these', 'strange', 'creatures', 'will', 'continue', 'to', 'exceed', 'our', 'expectations.']]

    I want all the words in 1 list, how to fix that?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Use the extend method instead of append.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo