Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #defines what order to perform the functions, and sorts the list once created.
- def main():
- nameList = makeList()
- printList(nameList)
- nameList.sort()
- printList(nameList)
- newOutput(nameList)
- #searchNames(nameList)
- #creates the list from a text file
- def makeList():
- data = open('categories.txt', 'r')
- l = data.readlines()
- data.close()
- for i in l:
- i = i.lstrip('0123456789 ')
- return l
- #prints the list to the screen
- def printList(nameList):
- for n in nameList:
- print(n, end='')
- #creates a new output file, based on the first but in sorted order.
- def newOutput(nameList):
- newList = open('newOutput.txt', 'w')
- newList.writelines(nameList)
- newList.close()
- #search the list for a specific name. Includes a loop to allow a user to search multiple names.
- def searchNames(nameList):
- another = 'y'
- while another.lower() == 'y':
- search = input("What name are you looking for? (Use 'Lastname, Firstname', including comma: ") + '\n'
- if search in nameList:
- print("The name was found at index", nameList.index(search), "in the list.")
- another = input("Check another name? Y for yes, anything else for no: ")
- else:
- print("The name was not found in the list.")
- another = input("Check another name? Y for yes, anything else for no: ")
- #Call main function
- main()
- The text file I am iterating over is:This is basically the problem-- my i.rstrip isn't working right. What did I do wrong?
- #defines what order to perform the functions, and sorts the list once created.
- def main():
- nameList = makeList()
- printList(nameList)
- nameList.sort()
- printList(nameList)
- newOutput(nameList)
- #searchNames(nameList)
- #creates the list from a text file
- def makeList():
- data = open('categories.txt', 'r')
- l = data.readlines()
- data.close()
- for i in l:
- i = i.lstrip('0123456789 ')
- return l
- #prints the list to the screen
- def printList(nameList):
- for n in nameList:
- print(n, end='')
- #creates a new output file, based on the first but in sorted order.
- def newOutput(nameList):
- newList = open('newOutput.txt', 'w')
- newList.writelines(nameList)
- newList.close()
- #search the list for a specific name. Includes a loop to allow a user to search multiple names.
- def searchNames(nameList):
- another = 'y'
- while another.lower() == 'y':
- search = input("What name are you looking for? (Use 'Lastname, Firstname', including comma: ") + '\n'
- if search in nameList:
- print("The name was found at index", nameList.index(search), "in the list.")
- another = input("Check another name? Y for yes, anything else for no: ")
- else:
- print("The name was not found in the list.")
- another = input("Check another name? Y for yes, anything else for no: ")
- #Call main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement