我有一個文本文件,它看起來像這樣,在啓動線「距離IQC/E:距離XY」和遵循其餘的是一個長行,直至:END:我有一個文本文件,它是我想分解成字典的一行文本,我該怎麼做?
:BEGIN
"Distance IQC/E: Distance XY",0.09066,0.09060,0.00040,0.00040,0.00006,,"Distance IQC/F: Distance XY",0.14603,0.14590,0.00080,0.00080,0.00013,,"Distance IQC/G: Distance XY",0.12074,0.12070,0.00080,0.00080,0.00004,,"Distance IQC/I: Distance XY",0.21476,0.21600,0.00200,0.00200,-0.00124,,"Distance IQC/H: Distance XY",0.12714,0.12760,0.00080,0.00080,-0.00046,,"Distance IQC/N: Distance XY",0.08661,0.08690,0.00080,0.00080,-0.00029,,"Distance IQC/M: Distance XY",0.12997,0.13000,0.00080,0.00080,-0.00003,
:END
我想知道如何分割這個文本文件,使每一行以「距離」開始,其後的浮動廣告後面是下一個「距離」。
我可以使用file.replace(「:BEGIN」,「」)來擺脫開始和結束。
我要製作字典,然後將該字典重寫爲新的文本文件嗎?
請幫助我是一個非常新的程序員!
編輯:我希望可以將輸出爲:
Distance IQC/E: Distance XY 0.09066 0.09060 0.00040 0.00040 0.00006
Distance IQC/F: Distance XY 0.14603 0.14590 0.00080 0.00080 0.00013
Distance IQC/G: Distance XY 0.12074 0.12070 0.00080 0.00080 0.00004
Distance IQC/I: Distance XY 0.21476 0.21600 0.00200 0.00200 -0.00124
Distance IQC/H: Distance XY 0.12714 0.12760 0.00080 0.00080 -0.00046
Distance IQC/N: Distance XY 0.08661 0.08690 0.00080 0.00080 -0.00029
Distance IQC/M: Distance XY 0.12997 0.13000 0.00080 0.00080 -0.00003
這樣我可以清晰地歸檔數據轉換成Excel文件或類似的東西。
編輯2:
這裏是代碼的小位我到目前爲止:
with open("file.txt","r") as read_data:
f=read_data.read().replace(":BEGIN",'').replace(":END",'')
你應該閱讀一下[mcve]是什麼 - 如果你想要代碼,你需要向我們展示你到目前爲止的內容,以及哪些內容不起作用。也可能需要包含您所需的輸出。 –
你想要鑰匙是什麼?你想要什麼值?請提供您的預期輸出的例子。 – IanAuld
不需要一個字典來插入換行符:'astr = big_str.replace(「Distance」,「\ nDistance」)' – cdarke