首页 > 解决方案 > How do I make a python scrip to alphabetize these files. They are little complicated

问题描述

I have a file with text like this and would like it to be sorted first numerically and second alphabetically. I would like to make a python script for this but have failed.

Each of the items has a name which begins with a 't'.

This is an example of the file currently:

t24KAssetType = 
    (I1    (11)    "boats and boating equipment",
     I2    (12)    "bicycles",
     I3    (13)    "caravans, motorhomes, or trailers",
     I4    (14)    "sporting equipment",
     I5    (15)    "musical instruments",
     I6    (16)    "cameras and photographic equipment",
     I7    (17)    "hobby equipment")

t1Mths10Yrs =
    (I1     "12 months",
     I2     "10 years")  
      
tQAnotherSourceIncome =
        (I1     "Enter up to 5",
         I2     " ")

tDAllExpensesComma  =
        (I1     "after deducting all expenses,",
         I2     "")
         
tKExpenses = 
        (I1     " <underline> after</underline> expenses such as rates and insurance",
         I2     "")
         
tAgain =
        (I1    " again",
         I2    "")
         
tLots_Looking = 
        (I1    "Again, looking",
         I2    "Looking")
         
tApe_Remember = 
        (I1     "<BlueLight22>IF NECESSARY:</BlueLight22> Again", 
        I2      "Remember")
        
tzoo =
        (I1    " and ",
         I2    "")
         
tPool =
        (I1     "Apart from any owned or leased by the business or farm you work for, do",
         I2     "Do")
         
tStuff =  
        (I1  (11)  "jewellery", 
        I2   (12) "art works",
        I3   (13) "antiques",
        I4   (14) "collectibles",
        I5   (15) "other collectibles")

And here is an example of how i would like it to look:

t1Mths10Yrs =
    (I1     "12 months",
     I2     "10 years")

t24KAssetType = 
    (I1    (11)    "boats and boating equipment",
     I2    (12)    "bicycles",
     I3    (13)    "caravans, motorhomes, or trailers",
     I4    (14)    "sporting equipment",
     I5    (15)    "musical instruments",
     I6    (16)    "cameras and photographic equipment",
     I7    (17)    "hobby equipment")  
      
tAgain =
        (I1    " again",
         I2    "")

tApe_Remember = 
        (I1     "<BlueLight22>IF NECESSARY:</BlueLight22> Again", 
        I2      "Remember")
        

tDAllExpensesComma  =
        (I1     "after deducting all expenses,",
         I2     "")


         
tKExpenses = 
        (I1     " <underline> after</underline> expenses such as rates and insurance",
         I2     "")
                
tLots_Looking = 
        (I1    "Again, looking",
         I2    "Looking")
      
tPool =
        (I1     "Apart from any owned or leased by the business or farm you work for, do",
         I2     "Do")
    
tQAnotherSourceIncome =
        (I1     "Enter up to 5",
         I2     " ")
     
tStuff =  
        (I1  (11)  "jewellery", 
        I2   (12) "art works",
        I3   (13) "antiques",
        I4   (14) "collectibles",
        I5   (15) "other collectibles")

tzoo =
        (I1    " and ",
         I2    "")

Can anyone help please?

标签: pythonsorting

解决方案


因为你的特定格式我写了这段代码

with open("something.txt") as file:
    data = file.readlines()
    a={}
    for i in data:
        if i[0] !=" " and i != "\n":
            b=i[:-1]
            a[b]=[]
        elif i != "\n":
            if i[-1]==")":
                a[b].append(i)
            else:
                a[b].append(i[:-1])


with open("something.txt","w") as file:
    for i in sorted(a.keys()):
        file.write(i+"\n")
        for j in a[i]:
            file.write(j+"\n")

推荐阅读