Created
September 4, 2019 19:51
-
-
Save wcneill/ca9b9ad0d14f2891bc063c016c696ddc to your computer and use it in GitHub Desktop.
functions for reading txt files with two different formats and counting instances of certain strings in the files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# reads a file with a single name on each line and counts the | |
# number of instances of that name in the file | |
def getcount(path): | |
with open(path, 'r') as open_file: | |
name_count = {} | |
for line in open_file: | |
if line in name_count.keys(): | |
name_count[line] += 1 | |
else: | |
name_count[line] = 1 | |
for name, count in name_count.items(): | |
print('The name {} occurs {} times in the file'.format(name, count)) | |
# Reads a .txt file where each line is file path to an image. | |
# example: a/abbey/image_name.jpg | |
# Each line is parsed for the second folder in the directory, which is named | |
# by category. Each category is added as a key to a dictionary whose values are the | |
# number of instances of that category. | |
def getcats(path): | |
with open(path, 'r') as open_file: | |
cat_count = {} | |
for line in open_file: | |
category = line.split('/')[2] | |
if category in cat_count.keys(): | |
cat_count[category] += 1 | |
else: | |
cat_count[category] = 1 | |
for cat, count in cat_count.items(): | |
print('The number of appearences of {} in the file is {}'. format(cat, count)) | |
if __name__ == '__main__': | |
getcats('Training_01.txt') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment