我如何制作一个python scrip来按字母顺序排列这些文件.它们有点复杂



我有一个文件,里面有这样的文本,希望它先按数字排序,然后按字母顺序排序。我想为此制作一个python脚本,但失败了。

每个项目都有一个以"t"开头的名称。

这是当前文件的一个示例:

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")

这里有一个我希望它看起来如何的例子:

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    "")

有人能帮忙吗?

由于您的特定格式,我编写了此代码

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")

你可以试试这个:

with open('input.txt', 'rt') as file:
text = []
block = ''
for line in file:
if (line.lstrip().startswith('t')
and line.rstrip().endswith('=')):  # Start of new item-block
if block:
text.append(block)
block = ''
block += line
text.append(block.rstrip() + 'nn')  # Special treatment of last item-block
with open('output.txt', 'wt') as file:
file.write(''.join(sorted(text)).rstrip())

首先,文件被分割成项目块,这些项目块被收集在列表text中。然后,列表内容被写回排序(sorted(text)(到一个新文件中。关于新项目块何时开始的决定基于以't'(line.lstrip().startswith('t')(开始并以'='(line.rstrip().endswith('=')(结束的行。也许你可以只依赖第一部分,第二部分是预防措施。

最新更新