Python - Obtain A List Of Numbers From A File And Return As Dict Representation
Solution 1:
Something like this should suffice. Keep in mind that no validation has been done. Eg, blank lines, non-numeric characters. In your question is seemed like the numbers should be converted to an integer but your code doesn't so I included it anyway.
from collections import Counter
deffile_counts(filename):
# Open file for readingwithopen(filename, 'r') as file:
data = []
# Go through each line of the filefor line in file:
value = int(line)
data.append(value)
returndict(Counter(data))
if __name__ == '__main__':
filename = 'testfile.txt'print(file_counts(filename))
Issues you had were.
deffile_counts(filename):
a = open('filea.txt')
b = open('fileb.txt')
You are reading two files and ignoring the filename given as a parameter.
info = a.read()
This will read in the whole file, typically not the best when it comes to large files.
ifinfo== True:
info
will never be True
as it is a string.
return (dict(collections.Counter(info)))
This is typically fine, however you haven't formatted info as it is still a string, so your dictionary included the \n
characters, it doesn't work for digits with more than 1 character as it counts each individual character.
You are most likely getting an IOError. You need your text files in the same directory as your python file if you want to just use the filename, otherwise you have to supply the file path
Solution 2:
From your statement, I assume you received an IOError
such as:
IOError: [Errno 2] No such file ordirectory: 'filea.txt'
If you receive this error, it is because open
cannot find a file in the current working directory that matches the filename you are asking it to fetch. You need to add the path to the beginning of filename such as /home/username/project/filea.txt
to make sure python is searching in the correct directory.
Once you are able to open your file and are past the IOError, your code has a few kinks.
First, let's look at dict(collections.Counter(info))
>>> info = "100\n100\n3\n100\n9\n9\n"
>>> dict(collections.Counter(info))
{'1': 3, '0': 6, '3': 1, '\n': 6, '9': 2}
As we can see, collections.Counter()
is parsing each character in the string and counting the occurrence of each one. Therefore '1', '0' and '3' are each counted three times rather than counting 100 three times. Instead, you could make a list of the values as below:
>>> info = info.strip() # removes the last \n on the right>>> nums = info.split('\n') # breaks up the string into a list of elements>>> print(nums)
['100', '100', '3', '100', '9', '9']
>>> print(dict(collections.Counter(nums)))
{'9': 2, '100': 3, '3': 1}
You still have a few errors concerning your input and the if then statements at the end but I'll let you take a stab at them first. GL!
Post a Comment for "Python - Obtain A List Of Numbers From A File And Return As Dict Representation"