Skip to content

Instantly share code, notes, and snippets.

@nightwarriorftw
Last active April 20, 2020 20:31
Show Gist options
  • Save nightwarriorftw/550c6d29898cb83513e3e840a83779f9 to your computer and use it in GitHub Desktop.
Save nightwarriorftw/550c6d29898cb83513e3e840a83779f9 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
def getNewList(empList):
# strip the new line and double characters from the element present in the list
empList = [name.strip().strip('"') for name in empList]
# Generate the list in the required format i.e `last_name, first_name`
newList = ["{last}, {first}".format(
last=name.split()[1], first=name.split()[0]) for name in empList]
return newList
def main():
empList = []
# Read from the file and generating a list of employee names
try:
with open("list.txt", "r") as fp:
for line in fp:
empList.append(line)
except FileNotFoundError:
print("File that contains list of employees does not exist")
# Create an Employee object and get the list of objects in required format
newList = getNewList(empList) # New list
# Writing names again into a file in the required format i.e `last_name, first_name`
try:
with open("newList.txt", "w") as fp:
for name in newList:
fp.write("{name}\n".format(name=name))
except Exception as e:
print("Operation failed. Aborting !!{error}".format(error=e))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment