python - 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?
解决方案
因为你的特定格式我写了这段代码
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")
推荐阅读
- javascript - Variable is used before being assigned with fetch
- python - Curve_Fit not accurate
- angular - mat-checkbox inside nested mat-table does not change state
- swiftui - Edit Text Editor SwiftUI
- javascript - Discord.js:异步消息冷却/反垃圾邮件
- c# - 在 ASMX WebService 中使用 HttpClient
- python-3.x - 用于消除图像中阴影的颜色校正
- python - 在python中将行向量附加到保存的数组
- python - Selenium CSS 选择器(适用于 Scrapy 但不适用于 Selenium)Python
- python - 如何在 PySpark 中引导样本进行积极的未标记学习?