Home > Enterprise >  How can I take a list input from a file and turn it into a dictionary?
How can I take a list input from a file and turn it into a dictionary?

Time:11-01

So I've been looking all over StackOverflow for a way to properly solve my issue, but I haven't found anything suitable.

I am taking in a file that has words with associated values in the format of:

alone,1  
amazed,10  
amazing,10  
bad,1  
etc.  

I am taking in this text file and reading the lines, which breaks each of the lines into a list. I then need to transfer this list into a dictionary type, where the keyword and value are kept associated.

I found a solution for this problem in another question, but it currently gives an output that includes \n within the value part of the dictionary.
Here is the code:

keywords_file = open('keywords.txt')
keywords = keywords_file.readlines()

def keyword_to_dictionary(keywords):
    result = [{}]
    for item in keywords:
        key, val = item.split(",", 1)
        if key in result[-1]:
            result.append({})
        result[-1][key] = val
    return result

Output:

[{'alone': '1\n', 'amazed': '10\n', 'amazing': '10\n', 'bad': '1\n', 'best': '10\n',
'better': '10\n', 'excellent': '10\n', 'excited': '10\n', 'excite': '10\n', 'excites':
'10\n', 'exciting': '10\n', 'glad': '10\n', 'god': '5\n', 'good': '7\n', 'great': '7\n',
'greatest': '10\n', 'haha': '5\n', 'hahaha': '5\n', 'happy': '10\n', 'hate': '1\n',
'hurt': '1\n', 'hurts': '1\n'}]

The output is longer but I hope that gives a decent idea of what is happening. I understand the issue, but I'm not sure how to go about solving it.

To give some context in how this is going to be used, I have a comp sci assignment that requires me to equate a few thousand lines of twitter data with keywords in this file, outputting a sort of happiness level average that is going to be associated with the geographical area that the tweet was sent in, determined by timezones. I need to be able to access the keywords and equated value in order to do this and figured a dictionary was the best way to do that.

context tldr: need to access the keyword and value associated with it when iterating through thousands of lines of Twitter data.

Any help would be greatly appreciated.

Apologies if there is anything wrong with this post or more information is required which I didn't provide.

Thanks in advance.

CodePudding user response:

You can use a dictionary comprehension:

with open('keywords.txt') as f:
    result = {k: int(v) for line in f for k,v in [line.strip().split(',')]}
  • Related