Skip to content

Instantly share code, notes, and snippets.

@sshehrozali
Created December 30, 2020 08:21
Show Gist options
  • Save sshehrozali/aac4d2949801fec148c03f0cd650af68 to your computer and use it in GitHub Desktop.
Save sshehrozali/aac4d2949801fec148c03f0cd650af68 to your computer and use it in GitHub Desktop.
Solution for given Question in paper.
# Dictionary
dic = {"SECP": "Securities and Exchange Commission Pakistan", "SBP": "State Bank of Pakistan", "FBR": "Federal Board of Revenue", "PCB": "Pakistan Cricket Board", "ISPR": "Inter Services Public Relations"}
# Open file in read mode
fp = open("Lab 8/20B - 084 - SE.txt", "r")
# Empty list to store list of lines
lst_lines = []
# Get each line of file
for line in fp:
# Store in list removing the new-line character ('\n')
lst_lines.append(line.rstrip('\n'))
# Open new file with same name in write mode
fp = open("20B - 084 - SE.txt", "w")
# Iterate through whole list
for i in range(len(lst_lines)):
# Iterate through each keys inside dictionary
for item in dic:
# Check if value of that key matches with ith member of list
if dic[item] == lst_lines[i]:
# If YES
# Check if last member of list (don't add '\n')
if i == len(lst_lines) - 1:
line = f"{lst_lines[i]}\t({item})"
fp.write(line)
break
# If not last member of list (add '\n')
else:
line = f"{lst_lines[i]}\t({item})\n"
fp.write(line)
break
# If not matches, directly write to the file
else:
line = f"{lst_lines[i]}\n"
fp.write(line)
# Close the file
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment