Click here to Skip to main content
14,392,909 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to change this list into a dictionary, i have such a list:

['2009-08-31 23:59:35 ', 'RT @brahmresnik: Pastor v Obama: Leading national tracker of hate groups taking close look at Tempe pastor on 12 News at 5. #NOH8\n']



I want to make the date as the first element with the key "date", and the tweet as the second element with they key tweet

i wrote the following code:
with open('newdata.txt') as f:
    for line in f:
        tweet=re.sub(r'T\t',"",line)
        t=re.sub(r'W\t',"\/\/",tweet)
        data=t.split("\/\/")
        print(data)
        d = [{'Date': x[0], 'Tweet': x[1]} for x in data]
        print(d)


But the ouput is this:
[{'Date': '2', 'Tweet': '0'}, {'Date': 'b', 'Tweet': 'a'}]


What I have tried:

Following this link Python - convert list to dict - Stack Overflow[^]
Posted
Updated 7-Jun-18 0:45am
Comments
Richard MacCutchan 7-Jun-18 7:48am
   
You should print the results after each line of code, to see what is being transformed by your regex and split. Also, if you have some code from StackOverflow that does not work then you should reply to the StackOverflow message.
Member 13647869 10-Jun-18 1:42am
   
sorry but i only took one line from StackOverflow, so that does not mean i reply to the StackOverflow message
Richard MacCutchan 10-Jun-18 6:02am
   
Well since we do not know what the StackOverflow code does we cannot answer. Did you follow my first suggestion?
Rahul VB 27-Mar-19 11:06am
   
Do you want to do something like below:
l = ['2009-08-31 23:59:35 ', 'RT @brahmresnik: Pastor v Obama: Leading national tracker of hate groups taking close look at Tempe pastor on 12 News at 5. #NOH8\n']
d = {}
>>> d[l[0]] = l[1]
>>> d
{'2009-08-31 23:59:35 ': 'RT @brahmresnik: Pastor v Obama: Leading national tracker of hate groups taking close look at Tempe pastor on 12 News at 5. #NOH8\n'}
>>> d['2009-08-31 23:59:35 ']
'RT @brahmresnik: Pastor v Obama: Leading national tracker of hate groups taking close look at Tempe pastor on 12 News at 5. #NOH8\n'
>>>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month