1

Searched for this but the answers dont seem to apply to my case. For some reason, the list returned is acknowledging that there are multiple lines, but return the same dictionary over and over again...here's my code:

import io with io.open(filename, "r", encoding="utf-8") as my_file: #empty list for storing dictionaries

    tweet_list=[]
    tweet={}
#for loop to read and format txt line
    x= 0
    for readtweet in my_file:
        #print(readtweet)
        import datetime as dtime
        #**REMEMBER TO COME BACK & ADD CODE THAT SKIPS STUFF WOUT 
        #read line
        #tweet1 = my_file.readline()
        #make it a list
        tw= readtweet.split('\t')
        #print(tw)
        #format coordinates
        coord_list= tw[0].split(',')
        lat_format= coord_list[0].strip('[')
        long_format= coord_list[1].strip(']')
        latitude= float(lat_format)
        longitude= float(long_format)
        #store tweet context as 'text'
        text= str(tw[3])
        #print(text)
        #format time
        dt=tw[2].split()
        dt[0]=dt[0].split('-')
        dt[1]=dt[1].split(':')
        year= int(dt[0][0])
        month= int(dt[0][1])
        day= int(dt[0][2])
        hour= int(dt[1][0])
        minute= int(dt[1][1])
        seconds= int(dt[1][2])
        time= dtime.datetime(year,month,day,hour,minute,seconds)
    #for loop for assigning value
        tweet['text']= text
        tweet['time']= time
        tweet['latitude']= latitude
        tweet['longitude']= longitude

        #print(tweet)
    #for loop for adding dict to  list
        tweet_list.append(tweet)

return(tweet_list)
shayku7
  • 35
  • 6
  • use `tweet_list.append(tweet.copy())` or `tweet[:]` - your are adding a "reference" of `tweet` to your `tweet_list` - and that multiple times. By copying `tweet` at the time you add it, you add different things to it – Patrick Artner Nov 11 '17 at 19:26
  • see [how-to-clone-or-copy-a-list](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) - if copy() is not enough ( elements inside still references) use `deepcopy()` - see the 18xx voted answer in the link – Patrick Artner Nov 11 '17 at 19:29
  • If that solves your propblem, consider removing the question - answer was on SO alreay - for me you are the 2nd one stumbling over this today. If you want to leave it, make/mark answer yourself. – Patrick Artner Nov 11 '17 at 19:39

0 Answers0