Skip to content

Instantly share code, notes, and snippets.

@Venkat-Swaraj
Created June 28, 2022 14:35
Show Gist options
  • Save Venkat-Swaraj/46da155d0a04451a0a80dab23fa977b2 to your computer and use it in GitHub Desktop.
Save Venkat-Swaraj/46da155d0a04451a0a80dab23fa977b2 to your computer and use it in GitHub Desktop.
Files-Replit
def category_dict(filename):
# Write your logic here
dictionary = {}
fp = open(filename,'r')
names = fp.readlines()
for name in names:
words = name.split(' ')
words[1] = words[1].strip('\n')
if words[1] in dictionary:
dictionary[words[1]].append(words[0])
else:
dictionary[words[1]] = [words[0]]
return dictionary
if __name__=="__main__":
print(category_dict("places1.txt"))
def file_to_dict(filename):
dictionary = {}
# Write your logic here
fp = open(filename,'r')
lines = fp.readlines()
for line in lines:
dict = line.split(" - ")
words = dict[1].split(', ')
for i in range(len(words)):
words[i] = words[i].strip()
words[i] = words[i].strip('\n')
dictionary[dict[0]] = words
return dictionary
def main():
filename = input()
print(file_to_dict(filename))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment