Ignoring quotes while sorting lists in Python? -


i making program read file, alphabetize info, , paste output.. issue having in information begins quotes ("").

the main function program auto-sort mla works cited pages (for fun obviously).

here code... love criticism, suggestions, opinions (please keep in mind first functioning program)

tl;dr -- how ignore " 's , still alphabetize data based on next characters..

code:

import os, sys  #list text mainlist = [] manlist = []  #definitions def fileread():   open("input.txt", "r+") f:     newline in f:       str = newline.replace('\n', '')       #print(str)       manlist.append(str)   mansort(manlist)   #print("debug")   #print(manlist)  def main():   print("input data(type 'done' when complete or type 'manual' file-read):")   x = input()   if x.lower() == 'done':     sort(mainlist)   elif x == '':     print("you must type something!")     main()   elif x.lower() == 'manual':     fileread()   else:     mainlist.append(x)     main()   def mansort(manlist):   print("what name file?(exit terminate):")    filename = input()   manlist = sorted(manlist, key=str.lower)   s in manlist:     finalstring2 = '\n'.join(str(manlist) manlist in manlist)   if filename == '':     print("you must choose name!")   elif filename.lower() == 'exit': sys.exit()   else:     open(filename + ".txt", "w+") f:       f.write(str(finalstring2))   def sort(mainlist):   os.system("cls")   mainlist = sorted(mainlist, key=str.lower)   s in mainlist:     finalstring = '\n'.join(str(mainlist) mainlist in mainlist)   print(finalstring)    print("what name file?(exit terminate):")    filename = input()    if filename.lower() == 'exit':     sys.exit()   elif  filename == '':     print("you must type something!")     sort(mainlist)   else:     open(filename + ".txt", "w+") f:       f.write(str(finalstring))    print("\npress enter terminate.")   c = input()   main()   #clears prevent spam. os.system("cls") 

please keep criticism constructive... also, example, want "beta" come after alpha, current program, come first due "" 's

sorted(mainlist, key=str.lower) 

you've figured out can perform transformation on each item on mainlist, , sort "mapped" value. technique known schwartzian transform.

just go 1 step further - remove quotes and convert lower case.

sorted(mainlist, key=lambda s: s.strip('"').lower()) 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -